views:

202

answers:

2

Hi,

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?

A: 

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
A: 

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.

Dimestore Cowboy
-1: how does this answer the question?
John Saunders