Why won't this edit code work?

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


dbCommand.Parameters.Add("@showdate", SqlDbType.Date).Value = txt_date.Text; 


Should be:


dbCommand.Parameters.Add("@showdate", SqlDbType.DateTime).Value = txt_date.Text;

Its expecting a DateTime and you are setting the value of the parameter as a string

DateTime.TryParse(txt_date.Text,…)

Hey,

It’s the date. I have date field as Date in my database.

Now i have a date drop down where i populate the textbox and enters the date like to: 13/05/2010

When i insert i do this:


    protected void Button1_Click(object sender, EventArgs e)
    {
            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();

            Response.Redirect("admin-articles.aspx");
    }

And then i get this error:

String was not recognized as a valid DateTime.

Even though the field is Date in my database.

Any ideas what i’m doing wrong?

Thanks

billy has a try/finally not a try/catch, but Redirecting in the finnaly block prevents one from seeing the error.

Any way i’m no sure if this line will parse the query string or just makes a cast to int witch will fail.

System.Convert.ToInt32COLOR=#007700; [/COLOR]

By swollowing errors, he means that everything is in a try{} catch{} block. And thus not showing an error message. Take your error catching off by removing the try/catches. The framework will then throw and error when something goes wrong and if you post that, we can try and help further

Yeah, you have just try/finally block, If you comments out the Response.Redirect, the error should be thrown

I don’t understand what you mean?

I can’t find any errors i don’t get an error message the update statement just does not work?

Can you see something in my code that i am doing wrong?

Please be specific, thanks.

Thanks again

Don’t swallow errors, which your doing in your button handler. Fix that and post your error