?
Solved

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

Posted on 2007-10-03
12
Medium Priority
?
698 Views
Last Modified: 2012-05-05
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!!
0
Comment
Question by:JeffreyDurham
  • 6
  • 6
12 Comments
 
LVL 21

Expert Comment

by:surajguptha
ID: 20010712
Why dont you use a DB sequence to do this, instead of manually doing it?
0
 

Author Comment

by:JeffreyDurham
ID: 20010722
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
 
LVL 21

Expert Comment

by:surajguptha
ID: 20010735
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:JeffreyDurham
ID: 20010749
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
 
LVL 21

Expert Comment

by:surajguptha
ID: 20010784
Never mind. Lets try to make ur code work...
Does InvoiceNum() return some values?



0
 

Author Comment

by:JeffreyDurham
ID: 20010798
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
 
LVL 21

Expert Comment

by:surajguptha
ID: 20010847
>> 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
 

Author Comment

by:JeffreyDurham
ID: 20010912
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
 
LVL 21

Expert Comment

by:surajguptha
ID: 20010932
You are using the CreateUserWizard1.CreateUserStep... Is this a third party component?
0
 

Author Comment

by:JeffreyDurham
ID: 20010953
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
 
LVL 21

Accepted Solution

by:
surajguptha earned 2000 total points
ID: 20010958
Well can you include the invoice number into that query in the datasource??? That way it can take in a invoice number
0
 

Author Comment

by:JeffreyDurham
ID: 20011005
Okay... well that was stupid mistake on my part... thanks for the help!  
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Suggested Courses
Course of the Month16 days, 18 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question