Link to home
Start Free TrialLog in
Avatar of Jeanette Durham
Jeanette DurhamFlag for United States of America

asked on

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!!
Avatar of surajguptha
surajguptha
Flag of United States of America image

Why dont you use a DB sequence to do this, instead of manually doing it?
Avatar of Jeanette Durham

ASKER

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?
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
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?
Never mind. Lets try to make ur code work...
Does InvoiceNum() return some values?



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
>> 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
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)


   
You are using the CreateUserWizard1.CreateUserStep... Is this a third party component?
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>
ASKER CERTIFIED SOLUTION
Avatar of surajguptha
surajguptha
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Okay... well that was stupid mistake on my part... thanks for the help!