0

I have used the following code to add the feedback entered to the web form into a database.

The code works fine. But when I try to deleted thi statement

SqlCommandBuilder objcb = new SqlCommandBuilder(objDa);

I am getting an error I mean it is executing the else part.

Can any one tell me what is the use of SqlCommandBuilder?

protected void btnSubmit_Click(object sender, EventArgs e)
{
    if (txtName.Text.Trim() == "")
    {
        Response.Write("<script>alert ('Name Field cannot be left blank')</script>");
        return;            
    }

    if (txtFeedBack.Text.Trim() == "")
    {
        Response.Write("<script>alert('FeedBack Field cannot be left blank')</script>");
        return;
    }

    objSqlConnection.ConnectionString = connectionStringSetting;
    objSqlConnection.Open();

    try
    {
        SqlDataAdapter objDa = new SqlDataAdapter("select * from FeedBack", objSqlConnection);
        SqlCommandBuilder objcb = new SqlCommandBuilder(objDa);
        DataSet objDs = new DataSet("FeedBack");
        objDa.Fill(objDs, "FeedBack");
        DataRow dr = objDs.Tables["FeedBack"].NewRow();
        dr[1] = txtName.Text;
        dr[2] = txtAddress.Text;
        dr[3] = txtCity.Text;
        dr[4] = txtCountry.Text;
        dr[5] = txtEmail.Text;
        dr[6] = Convert.ToInt32(txtContactNo.Text);
        dr[7] = txtFeedBack.Text;
        objDs.Tables["FeedBack"].Rows.Add(dr);
        objDa.Update(objDs, "FeedBack");
        Response.Write("<script>alert('Your FeedBack has been submitted')</script>");
        objSqlConnection.Close();
    }
    catch (Exception)
    {
        Response.Write("<script> alert('Error on Page. Please try after sometime')</script>");
        objSqlConnection.Close();
    }

And is there any way to display the specific error messages like if an user enters a string value instead of Integer value it should display the message as 'Input String not entered in correct format?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Sheetal
  • 853
  • 6
  • 15
  • 22

2 Answers2

0

Never use catch (Exception). It hides the problem. If an exception is being thrown, then there's something serious wrong. You need to learn about it.

Remove the entire try/catch block (keep the code inside it!). Then run your code again. If there's an exception, then you'll see it on the error page. If not, then use the Event Viewer to look in the Windows Event Log for a warning message from the "ASP.NET" source. It should contain the complete exception.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • While I agree with the spirit of your answer, telling someone to 'NEVER use catch(Exception)' seems a bit misleading to me. I guess I would encourage people to properly learn how to effectively use try/catch blocks, and to understand when it is appropriate to handle Exceptions, and when to let them bubble up and get handled by the runtime. – Chris Jaynes Apr 04 '12 at 14:42
  • In almost all cases, exceptions should _not_ be handled. That's especially true in the way the OP caught the exception, and then didn't even display it. I'd rather tell someone _never_, and wait until they learn that they _must_ catch exceptions in some case, rather than saying "maybe" and having them think, "all the time". – John Saunders Apr 04 '12 at 15:51
  • Like I said, I agree with the spirit of your comment. I think the big problem is that so many examples needlessly catch and print the Exception, so people just cut and paste, and few people ever learn how Exception handling is supposed to work. – Chris Jaynes Apr 04 '12 at 19:13
0

Final answer is that the line you are talking out is doing a bunch of stuff in the background.

It is adding the insert, update and delete commands to the DataAdapter. So without it, you will crash and burn, unless you add those commands yourself to the DataAdapter.

abatishchev
  • 98,240
  • 88
  • 296
  • 433