0

Creating a ASP.NET form with C#, i am facing this error i don't know what is the error with it. Its all doing fine but when i push the save Button it gives me this error:

     NulllRefrenceException was unhandled by user code
    {"Object reference not set to an instance of an object."}
Object reference not set to an instance of an object.

Code:

     protected void Button8_Click(object sender, EventArgs e)
    {
         SqlConnection cnn = new SqlConnection();
         cnn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["sqlAddSave"].ConnectionString;
    cnn.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "select * from  DisplayPP";
    cmd.Connection = cnn;
    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = cmd;
    DataSet ds = new DataSet();
    da.Fill(ds, " DisplayPP ");
    SqlCommandBuilder cb = new SqlCommandBuilder(da);
    DataRow drow = ds.Tables["DisplayPP"].NewRow();
    drow["website"] = web.Text;
    drow["country"] = DropDownList1.SelectedItem.Text;
    drow["contact"] = TextBox144.Text;
    drow["cat"] = TextBox145.Text;
    drow["traff"] = TextBox146.Text;

    more text boxes as above

    ds.Tables["DisplayPP "].Rows.Add(drow);
    da.Update(ds, " DisplayPP ");
    string script = @"<script language=""javascript"">
    alert('Information have been Saved Successfully.......!!!!!.');
   </script>;";
    Page.ClientScript.RegisterStartupScript(this.GetType(), "myJScript1", script);
    }

please help.

Connection String:

<add name="sqlAddSave" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\PPTableDisplay.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />

Exception

Exception Details: System.NullReferenceException was unhandled by user code HResult=-2147467261 Message=Object reference not set to an instance of an object. Source=TestCRole StackTrace: at TestCRole._Default.Button8_Click(Object sender, EventArgs e) in c:\Users\xxxxx\Documents\Visual Studio 2012\Projects\WindowsAzure2\TestCRole\Default.aspx.cs:line 60 at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) InnerException:

11
  • 1
    Where do you get the error? Commented Jul 23, 2013 at 18:24
  • 2
    Use the debugger to find out what is null. Commented Jul 23, 2013 at 18:24
  • 7
    TextBox146.Text You should name your controls. Commented Jul 23, 2013 at 18:25
  • Please, put a breakpoint on the first line and debug step by step (using F10). This way you'll figure out which object, exactly, is not set. Commented Jul 23, 2013 at 18:25
  • @Slaks what you mean by TextBox146.Text should name your control ??? Commented Jul 23, 2013 at 18:26

1 Answer 1

1
ds.Tables["DisplayPP "].Rows.Add(drow);

should be this

ds.Tables["DisplayPP"].Rows.Add(drow);
Sign up to request clarification or add additional context in comments.

9 Comments

He's having the exception thrown before this line.
@Dimitar Dimitrov he is correct i was having same point error.
@JohnKim Oh, well, you said different in your comments :) Nevermind
Thanks mate ! I fixed that as it was same creating error in sqlconneciton string before.. well i have to add ID also ?? do I ?? means ID is auto generated so i left it as it was.. and got that error: Cannot insert the value NULL into column 'Id', table 'C:\USERS\XXXXXXX\DOCUMENTS\VISUAL STUDIO 2012\PROJECTS\WINDOWSAZURE2\TESTCROLE\APP_DATA\PPTABLEDISPLAY.MDF.dbo.DisplayPP'; column does not allow nulls. INSERT fails.
Is your Id column supposed to be an identity but you don't have it set as such? If you don't set the identity property it won't autopopulate the field when you add a record and this error will occur
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.