Hey,
I am pre-populating a set of text boxes with rows from a database and then i have a update function on button click. But it doesn’t always work. This is my code:
This is the SELECT on page load:
if (!IsPostBack)
{
///////////////////// C O N T E N T //////////////////////////////
string connStr = "Data Source=tcp:esql2k801.discountasp.net;Initial Catalog=SQL2008_673062_kids;User ID=SQL2008_673062_kids_user;Password=******;";
SqlConnection dbConn = new SqlConnection(connStr);
string sqlStr = "SELECT * FROM tbl_diary_articles WHERE article_id = @article_id";
dbConn.Open();
SqlCommand dbCommand = new SqlCommand(sqlStr, dbConn);
dbCommand.Parameters.Add("@article_id", SqlDbType.Char).Value = Request.QueryString["ID"].ToString();
SqlDataReader dbReader = dbCommand.ExecuteReader();
while (dbReader.Read())
{
txt_title.Text = dbReader["title"].ToString();
txt_body.Text = dbReader["body"].ToString();
txt_date.Text = dbReader["showdate"].ToString();
DropDownList1.SelectedValue = dbReader["month"].ToString();
DropDownList2.SelectedValue = dbReader["nursery_id"].ToString();
}
dbReader.Close();
///////////////////// E N D /////////////////////////////////////////
}
Then on button click i have the following:
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string connStr = "Data Source=tcp:esql2k801.discountasp.net;Initial Catalog=SQL2008_673062_kids;User ID=SQL2008_673062_kids_user;Password=******;";
SqlConnection dbConn = new SqlConnection(connStr);
string sqlStr = "UPDATE tbl_diary_articles SET [title] = @title, [body] = @body, [month] = @month, [nursery_id] = @nursery_id, [showdate] = @showdate WHERE [article_id] = @article_id";
SqlCommand dbCommand = new SqlCommand(sqlStr, dbConn);
dbCommand.Connection.Open();
dbCommand.Parameters.Add("@article_id", SqlDbType.Int).Value = System.Convert.ToInt32(Request.QueryString["ID"].ToString());
dbCommand.Parameters.Add("@title", SqlDbType.Char).Value = txt_title.Text;
dbCommand.Parameters.Add("@body", SqlDbType.Char).Value = txt_body.Text;
dbCommand.Parameters.Add("@month", SqlDbType.Char).Value = DropDownList1.SelectedValue;
dbCommand.Parameters.Add("@nursery_id", SqlDbType.Char).Value = DropDownList2.SelectedValue;
dbCommand.Parameters.Add("@showdate", SqlDbType.Date).Value = txt_date.Text;
dbCommand.ExecuteNonQuery();
dbCommand.Connection.Close();
}
finally
{
Response.Redirect("admin-articles.aspx");
}
}
Can anyone please help me find out what the problem may be and how i can find out specifically where the error is?
Thanks again