0
Answer

how to save the content (text and picture) of a richtextbox to a datatable and retrieve content from database?

Hello, I'm kinda new to C# and programming in general and this is the first database application that I'm working on. What I want to do is actually quite simple but I'm having a lot of trouble finding source code on it. I want to save the content (text and picture) of a rich text box to a datatable and I also want to be able to retrieve content I've saved into the database from it. These are two different codes that I tried (both for inserting) but there's no way I can be sure the first one worked and the second gives an exception...can someone help me out please? Your help would be greatly appreciated.

//Code to add from a textbox into a table. In this case, categories. Category spelt as Categorie intentional
  public void AddCategorie()
  { 
  //ConnectMe = new SqlConnection(connectionstring);
  //ConnectMe.Open();
  //CategorylistBox.BeginUpdate();
  //string C = "INSERT INTO CategoryTable (Category) VALUES ('" + CategorytextBox.Text.Replace("'", "") + "')"; //The replace method will replace any single quotation (') the user types with nothing, so "doesn't" will be "doesnt" so it won't affect our INSERT statement
  //MyCommand = new SqlCommand(C, ConnectMe);
  //MyCommand.ExecuteNonQuery();
 
  //this.categoryTableTableAdapter1.Fill(this.ktDatabaseDataSet1.CategoryTable);
  //this.Refresh();
  //CategorylistBox.SelectedIndex = CategorylistBox.Items.Count - 1;
  //CategorylistBox.EndUpdate();
  //ConnectMe.Close();
  SqlConnection myConnection = new SqlConnection(connectionstring);
  myConnection.Open();
  CategorylistBox.BeginUpdate();
  SqlCommand myCommand = myConnection.CreateCommand();
  MyCommand.CommandText = "INSERT INTO CategoryTable (Category) VALUES ('" + CategorytextBox.Text.Replace("'", "") + "')";
  MyCommand.CommandType = CommandType.Text;

  SqlDataReader myDataReader;
  myDataReader = myCommand.ExecuteReader();
  while (myDataReader.Read())
  {
  NoterichTextBox.Text = (myDataReader[1].ToString());
  }
  CategorylistBox.EndUpdate();
  myConnection.Close();
  }