I'm having trouble understanding what I'm doing wrong here. I've been able to select and populate forms with data from my SQL Server database with no problems. Now when I try to write back to the database through my modified dataset, nothing happens. Apparently, the update command does not work and I'm trying to find out why. Here's the code below.
[I'm a newbie to C# and SQL so i'll be very grateful if you can explain like I'm 5 :) ]
Edit: I'm 100% sure Its connected to the DB, retrieving data and populating the Dataset.
if (!(String.IsNullOrEmpty(Request.QueryString["newsID"])))
{
SqlDataAdapter UpdateNewsSDA = new SqlDataAdapter("SELECT newsID, newsTitle, newsAuthor, newsDate, shortContent, mainContent FROM news_Table WHERE newsID = @newsID", connectObj);
UpdateNewsSDA.SelectCommand.Parameters.Add("@newsID", SqlDbType.Int).Value = Convert.ToInt32(Request.QueryString["newsID"]);
UpdateNewsSDA.UpdateCommand = new SqlCommand("UPDATE news_table SET newsTitle=@newsTitle, newsAuthor=@newsAuthor, newsDate=@newsDate, shortContent=@shortContent, mainContent=@mainContent WHERE newsID=@newsID", connectObj);
UpdateNewsSDA.UpdateCommand.Parameters.Add("@newsID", SqlDbType.Int).Value = Convert.ToInt32(Request.QueryString["newsID"]);
UpdateNewsSDA.UpdateCommand.Parameters.Add("@newsTitle", SqlDbType.Text).Value = title_Textbox.Text;
UpdateNewsSDA.UpdateCommand.Parameters.Add("@newsAuthor", SqlDbType.Text).Value = author_Textbox.Text;
UpdateNewsSDA.UpdateCommand.Parameters.Add("@newsDate", SqlDbType.DateTime).Value = Convert.ToDateTime(date_Textbox.Text);
UpdateNewsSDA.UpdateCommand.Parameters.Add("@shortContent", SqlDbType.Text).Value = shortContent_Textbox.Text;
UpdateNewsSDA.UpdateCommand.Parameters.Add("@mainContent", SqlDbType.Text).Value = mainContent_Textbox.Text;
DataSet UpdateNewsDS = new DataSet();
SqlCommandBuilder UpdateNewsCommandBuilder = new SqlCommandBuilder(UpdateNewsSDA);
UpdateNewsSDA.MissingSchemaAction = MissingSchemaAction.AddWithKey;
UpdateNewsSDA.FillSchema(UpdateNewsDS, SchemaType.Source);
UpdateNewsSDA.Fill(UpdateNewsDS);
DataTable UpdateNewsTable = new DataTable();
UpdateNewsTable = UpdateNewsDS.Tables[0];
DataRow CurrentDR;
CurrentDR = UpdateNewsTable.Rows.Find(Convert.ToInt32(Request.QueryString["newsID"]));
CurrentDR.BeginEdit();
CurrentDR["newsAuthor"] = "Ron Weasely";
CurrentDR.AcceptChanges();
CurrentDR.EndEdit();
UpdateNewsSDA.Update(UpdateNewsDS);
}
Edit 2: I discovered the problem and its this whole block below!
UpdateNewsSDA.UpdateCommand = new SqlCommand("UPDATE news_table SET newsTitle=@newsTitle, newsAuthor=@newsAuthor, newsDate=@newsDate, shortContent=@shortContent, mainContent=@mainContent WHERE newsID=@newsID", connectObj);
UpdateNewsSDA.UpdateCommand.Parameters.Add("@newsID", SqlDbType.Int).Value = Convert.ToInt32(Request.QueryString["newsID"]);
UpdateNewsSDA.UpdateCommand.Parameters.Add("@newsTitle", SqlDbType.Text).Value = title_Textbox.Text;
UpdateNewsSDA.UpdateCommand.Parameters.Add("@newsAuthor", SqlDbType.Text).Value = author_Textbox.Text;
UpdateNewsSDA.UpdateCommand.Parameters.Add("@newsDate", SqlDbType.DateTime).Value = Convert.ToDateTime(date_Textbox.Text);
UpdateNewsSDA.UpdateCommand.Parameters.Add("@shortContent", SqlDbType.Text).Value = shortContent_Textbox.Text;
UpdateNewsSDA.UpdateCommand.Parameters.Add("@mainContent", SqlDbType.Text).Value = mainContent_Textbox.Text;
Apperently, my update command worked but quickly got replaced by the original content of the textbox by the code above.
Cheers.