Using SqlCommand.InsertParameter.Add to add a numeric field to a database from a webpage.

Hello Experts -

I am trying to create an invoice number that automatically increments each time an account is created.  I can get it to return the value fine, but everytime it tries to execute the line of code where it stores this value back into the DB for that users account I am getting an error.  Here is my code:

This creates the InvoiceNum, it works fine:


    private double InvoiceNum()
    {

        SqlDataSource DataSource = (SqlDataSource)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("InsertExtraInfo");
        string conString = DataSource.ConnectionString;
        SqlConnection cwConn = new SqlConnection(conString);
        cwConn.Open();
        MembershipUser User = Membership.GetUser(CreateUserWizard1.UserName);
        object UserGUID = User.ProviderUserKey;
        string myQuery = "SELECT Top 1 [InvoiceNum] from UserInfo ORDER BY [InvoiceNum] DESC;";
        SqlCommand invNumCommand = new SqlCommand(myQuery, cwConn);
        invNumCommand.CommandType = CommandType.Text;
        object objInvNum = invNumCommand.ExecuteScalar();
        cwConn.Close();
        cwConn.Dispose();
        double invNum = System.Convert.ToDouble(objInvNum);
        return  invNum + 1;
    }

This is my code to update the user account and enter the info into the UserInfo table:


    public void ExtraInfo(bool flagCCGood)
    {
        SqlDataSource DataSource = (SqlDataSource)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("InsertExtraInfo");
        MembershipUser User = Membership.GetUser(CreateUserWizard1.UserName);
        object UserGUID = User.ProviderUserKey;
        DataSource.InsertParameters.Add("UserId", UserGUID.ToString());
        DataSource.InsertParameters.Add("DateRegistered", DateTime.Now.ToString());
        DataSource.InsertParameters.Add("IPAddress", Request.UserHostAddress.ToString());
        DataSource.InsertParameters.Add("ActiveUser", flagCCGood == true ? "1" : "0" );
        string invoiceNumChk = Convert.ToString(InvoiceNum());  // works fine, returns the value of InvoiceNum
        DataSource.InsertParameters.Add("InvoiceNum", invoiceNumChk); // still shows the value
        DataSource.Insert(); // crashes here.  giving the following error:
}

 Cannot insert the value NULL into column 'InvoiceNum', table 'master.dbo.UserInfo'; column does not allow nulls. INSERT fails.
The statement has been terminated.

I am clearly getting a value for this field and have converted to a string like the insertparameter wants.  But somehow or another I am losing it once it goes to update?  the datatype in the SQL DB is a nchar(10) that does not accept nulls.

Thanks for any help!!
JeffreyDurhamAsked:
Who is Participating?
 
surajgupthaCommented:
Well can you include the invoice number into that query in the datasource??? That way it can take in a invoice number
0
 
surajgupthaCommented:
Why dont you use a DB sequence to do this, instead of manually doing it?
0
 
JeffreyDurhamAuthor Commented:
This is really the only way I know how to do this.  This all happens on a CreateUserWizard after the user is created and a payment is processed.  Can you explain to me what you mean by DB sequence?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
surajgupthaCommented:
It is a value that automatically gets incremented everytime you need to use a new order number or purchase number or membership number basically any primary unique number
0
 
JeffreyDurhamAuthor Commented:
I am very new to SQL.  I think you are talking about something similar to an autonumber field like Access has?  Can you explain how I should implement this?
0
 
surajgupthaCommented:
Never mind. Lets try to make ur code work...
Does InvoiceNum() return some values?



0
 
JeffreyDurhamAuthor Commented:
Yes, it returns 12304.  Which is the highest value in my invoice number column + 1.  That is the strangest part is that it does return a value, that I convert to a string, but once it tries to insert it errors out and says it is inserting a null
0
 
surajgupthaCommented:
>> Can you check the DataSource object to see the actual insert SQL command ??
>> Also can you check to see which of the parameters you might be passing as null?

My guess is to do with the sequence of parameters passed
0
 
JeffreyDurhamAuthor Commented:
I did a response.write of my insert command and it returned the following.  I had to turn on Allow Nulls in that column to get it to run.  This is very strange as well, that column doesn't even show up and I did not remove it from the code.  It is definately the InvoiceNum that is being passed as a null.  It states in the error as well.

INSERT INTO [UserInfo] ([UserID], [BillingName], [BillingCompany], [BillingAddress], [BillingCity], [BillingState], [BillingZip], [BillingPhone], [Email], [SubscriptionType], [DateRegistered], [IPAddress], [ActiveUser]) VALUES (@UserID, @BillingName, @BillingCompany, @Address, @City, @State, @Zip, @Phone, @Email, @SubscriptionType, @DateRegistered, @IPAddress, @ActiveUser)


   
0
 
surajgupthaCommented:
You are using the CreateUserWizard1.CreateUserStep... Is this a third party component?
0
 
JeffreyDurhamAuthor Commented:
The SQL Datasource I have nested in the CreateUserWizard itself right now and that is how I pulled out the connection.  This is what I have in my ASP page.  It also contains values I pass from the wizard itself.

 <asp:SqlDataSource ID="InsertExtraInfo" runat=server ConnectionString="<%$ ConnectionStrings:LocalSQLServer %>"
    InsertCommand="INSERT INTO [UserInfo] ([UserID], [BillingName], [BillingCompany], [BillingAddress], [BillingCity], [BillingState], [BillingZip], [BillingPhone], [Email], [SubscriptionType], [DateRegistered], [IPAddress], [ActiveUser])&#13;&#10;        &#13;&#10;        VALUES (@UserID, @BillingName, @BillingCompany, @Address, @City, @State, @Zip, @Phone, @Email, @SubscriptionType, @DateRegistered, @IPAddress, @ActiveUser)" >
   
    <InsertParameters>

        <asp:ControlParameter Name="BillingName" Type="String" ControlID="BillingName" PropertyName="Text" />
        <asp:ControlParameter Name="BillingCompany" Type="String" ControlID="Company" PropertyName="Text" />
        <asp:ControlParameter Name="Address" Type="String" ControlID="Address" PropertyName="Text" />
        <asp:ControlParameter Name="City" Type="String" ControlID="City" PropertyName="Text" />
        <asp:ControlParameter Name="State" Type="String" ControlID="State" PropertyName="Text" />
        <asp:ControlParameter Name="Zip" Type="String" ControlID="Zip" PropertyName="Text" />
        <asp:ControlParameter Name="Phone" Type="String" ControlID="Phone" PropertyName="Text" />
        <asp:ControlParameter Name="Email" Type="String" ControlID="UserName" PropertyName="Text" />
        <asp:ControlParameter Name="SubscriptionType" Type="String" ControlID="txtOrderAmount" PropertyName="SelectedValue" />

    </InsertParameters>  
    </asp:SqlDataSource>
0
 
JeffreyDurhamAuthor Commented:
Okay... well that was stupid mistake on my part... thanks for the help!  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.