当我尝试将文本框中的数据插入SqlDataSource时,它给出了错误“对象引用未设置为对象的实例”

时间:2013-05-02 03:21:43

标签: c# insert sqldatasource

protected void btnInsert_Click(object sender, EventArgs e)
{
            string custName = ddlCustomerName.SelectedValue;
            string listing = ddlListing.SelectedValue;
            sdsCustomers.InsertParameters["@CustomerID"].DefaultValue = sdsCustomerName.SelectParameters["CustomerID"].DefaultValue;
            sdsCustomers.InsertParameters["ListingID"].DefaultValue = sdsCustomerName.SelectParameters[listing].DefaultValue;
            sdsCustomers.InsertParameters["FullName"].DefaultValue = custName;
            sdsCustomers.InsertParameters["Date"].DefaultValue = txtBxDate.ToString();
            sdsCustomers.InsertParameters["Reason"].DefaultValue = ddlReason.SelectedValue;
            sdsCustomers.InsertParameters["BidPrice"].DefaultValue = txtBxBidPrice.Text;
            sdsCustomers.InsertParameters["CommissionRate"].DefaultValue = txtBxDate.Text;
            sdsCustomers.Insert();
}

你能告诉我这里我做错了什么吗?

SELECT
   CustAgentList.AgentID, CustAgentList.CustomerID, 
   Customers.LastName + ', ' + Customers.FirstName AS FullName, 
   CAST(CustAgentList.ListingID AS VARCHAR) + ', ' + Customers.Address + ', ' + Customers.City AS Listing, 
   CustAgentList.ContactDate AS Date, CustAgentList.BidPrice, 
   CustAgentList.CommissionRate, ContactReason.ContactReason AS Reason 
FROM 
   CustAgentList 
INNER JOIN 
   Customers ON CustAgentList.CustomerID = Customers.CustomerID 
INNER JOIN 
   ContactReason ON CustAgentList.ContactReason = ContactReason.ContactReason 
WHERE 
   (CustAgentList.AgentID = @AgentID) 
ORDER BY 
   Date

这是我的sdsCustomer insertCommand。使用此SqlDataSource的datagridview填充得很好。

1 个答案:

答案 0 :(得分:0)

您正在使用的其中一个控件可能会返回null。调用方法或访问null上的属性会产生NullReferenceException,与将null传递给多种方法相同。

如果您可以调试页面,只需在btnInsert_Click的第一行设置断点,并检查分配右侧的值,以便找到罪魁祸首。

或者获取异常的堆栈跟踪。还有其他方法可以做,但尝试在事件处理程序的修改版本中设置断点,仅用于调试,如下所示:

protected void btnInsert_Click(object sender, EventArgs e)
{
    try { // FORNOW: for debugging
        string custName = ddlCustomerName.SelectedValue;
        string listing = ddlListing.SelectedValue;
        sdsCustomers.InsertParameters["@CustomerID"].DefaultValue = sdsCustomerName.SelectParameters["CustomerID"].DefaultValue;
        sdsCustomers.InsertParameters["ListingID"].DefaultValue = sdsCustomerName.SelectParameters[listing].DefaultValue;
        sdsCustomers.InsertParameters["FullName"].DefaultValue = custName;
        sdsCustomers.InsertParameters["Date"].DefaultValue = txtBxDate.ToString();
        sdsCustomers.InsertParameters["Reason"].DefaultValue = ddlReason.SelectedValue;
        sdsCustomers.InsertParameters["BidPrice"].DefaultValue = txtBxBidPrice.Text;
        sdsCustomers.InsertParameters["CommissionRate"].DefaultValue = txtBxDate.Text;
        sdsCustomers.Insert();
    }
    catch (Exception ex) { // FORNOW: for debugging
          var exDetails = ex.ToString();
          ; // Set a breakpoint here, and inspect the value of exDetails.
    }
}