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:

4

1 回答 1

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

应该是这个

ds.Tables["DisplayPP"].Rows.Add(drow);
于 2013-07-23T18:40:16.977 回答