-2
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。使用它的 datagridviewSqlDataSource填充得很好。

4

1 回答 1

0

One of the controls you are using is likely returning null. Calling a method or accessing a property on a null yields a NullReferenceException, same with passing null to many methods.

If you can debug the page, just set a breakpoint on the first line of btnInsert_Click, and inspect the values on the right sides of the assignments to likely find the culprit.

Alternatively get the stack trace of the exception. There are other ways to do it, but try setting a breakpoint in a modified version of your event handler just for debugging like so:

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.
    }
}
于 2013-05-02T03:33:38.117 回答