Click here to Skip to main content
15,883,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am trying to insert data from textbox to SQL DB table.
When I am clicking on update button, getting below error

Server Error in '/' Application.

C#
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

 Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


C#
Line 81:         protected void Button2_Click(object sender, EventArgs e)
Line 82:         {
Line 83:             SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ToString());
Line 84:             try
Line 85:             {


C#
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ToString());
           try
           {
               string query = "insert into Master_Data(Track_Status,Track_MRBDIR, Track_MRBDIRVer,Track_IssueDescr,Track_SrlNo,Track_TOCDIR,Track_Checker,Track_CheckerSAPID,Track_Approver,Track_ApproverSAPID,Track_WP,Track_Material,Track_PriorityLvl,Track_CostCode,Track_MNQty,Track_ForecastD,Track_MRBStatus)values('" + DropDownList1.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "','" + TextBox12.Text + "','" + TextBox9.Text + "','" + TextBox13.Text + "','" + TextBox10.Text + "','" + TextBox11.Text + "','" + DropDownList2.Text + "','" + TextBox14.Text + "','" + TextBox16.Text + "','" + TextBox17.Text + "','" + DropDownList3.Text + "');";
               SqlDataAdapter da = new SqlDataAdapter(query, conn);
               conn.Open();
               da.SelectCommand.ExecuteNonQuery();
               conn.Close();
               Updatelbl.Visible = true;
           }
           catch
           {
               conn.Close();
               //lblmessage.text = "Error while saving data.";

           }
       }


need your expertise to get this issue down.

Thank You
Posted
Updated 12-Jan-16 22:33pm
v2
Comments
StM0n 13-Jan-16 4:32am    
Additional to my solution, you should never trust user-input... don't just insert the text from controls w/o checking/escaping them or use parameters.

Are you aware of the fact, that your connection you're instantiate is called con and the one you're trying to use is called conn?

BTW: put your con.close into a finally...
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 13-Jan-16 4:09am    
5ed; good catch!

But, I wonder where does that conn come from as it may be another instance of the connection. Otherwise, "conn" does not exist in current context may have been the error.
StM0n 13-Jan-16 4:30am    
I also wondered that... but maybe you're right, that there's a variable somewhere hiding :)
Check the line number you are getting this error. In exception also check for inner exception. This error mostly surface when the variable / control is been used before they are instantiated
 
Share this answer
 
please replace conn --> with con

C#
SqlDataAdapter da = new SqlDataAdapter(query, con);
               con.Open();
               da.SelectCommand.ExecuteNonQuery();
               con.Close();



then it works
 
Share this answer
 
v2
Comments
Dj@y 13-Jan-16 6:19am    
And one thing whenever you write wrong code it show compile error,
in your case it should be give compile time error.
for conn. variable

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900