{“你调用的对象是空的。”}

时间:2013-07-23 18:23:10

标签: asp.net sql nullreferenceexception

使用C#创建ASP.NET表单,我遇到此错误我不知道它的错误是什么。一切都很好,但是当我按下保存按钮时,它给了我这个错误:

     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.

代码:

     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);
    }

请帮忙。

连接字符串:

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

异常

  

异常详细信息:用户未处理System.NullReferenceException   code HResult = -2147467261 Message =对象引用未设置为   对象的实例。 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:第60行at   System.Web.UI.WebControls.Button.RaisePostBackEvent(字符串   event.Web.UI.Page.ProcessRequestMain的EventArgument(布尔值   includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)   的InnerException:

1 个答案:

答案 0 :(得分:1)

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

应该是这个

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