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

Can you tell me what am I doing wrong here?

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

This is my sdsCustomer insertCommand. The datagridview that uses this SqlDataSource is populated just fine.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
UGuess
  • 17
  • 4
  • yes, someone here could... can you show more about the error – G. Stoynev May 02 '13 at 03:28
  • Probably `sdsCustomers` is null. How you create it? – Xaqron May 02 '13 at 03:30
  • sdsCustomer is not null – UGuess May 02 '13 at 03:33
  • 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 – UGuess May 02 '13 at 03:34
  • 1
    use VS Debugger and see where exactly the error is thrown. – Prabhu Murthy May 02 '13 at 03:36
  • 1
    A friendly tip for you: if you have more code to share in response to comments, just update your question with it. Code formatting goes a long way to helping people chew on the details of your problem. – J0e3gan May 02 '13 at 03:37

1 Answers1

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.
    }
}
J0e3gan
  • 8,740
  • 10
  • 53
  • 80
  • I debugged it but I cannot find the value. I cannot even tell if the value I am getting is null. – UGuess May 02 '13 at 03:43
  • How do you get the data of a particular column from sqldatasource? – UGuess May 02 '13 at 03:47
  • Error is "Object reference not set to an instance of an object" – UGuess May 02 '13 at 03:55
  • Right. That much is clear. What is not clear is the source of the exception, which the exception's stack trace should reveal. – J0e3gan May 02 '13 at 03:56
  • I was trying another way. Can you tell me how do you get a single data, lets say customer name from Name column within sql data source? – UGuess May 02 '13 at 04:01
  • Bing's first result for *c# get column value from sqldatasource*: [http://forums.asp.net/t/1375564.aspx/1](http://forums.asp.net/t/1375564.aspx/1) I'd recommend just finding out straight from the horse's mouth as it were, though. :) – J0e3gan May 02 '13 at 04:20