Jeanette Durham
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)CreateUserW izard1.Cre ateUserSte p.ContentT emplateCon tainer.Fin dControl(" InsertExtr aInfo");
string conString = DataSource.ConnectionStrin g;
SqlConnection cwConn = new SqlConnection(conString);
cwConn.Open();
MembershipUser User = Membership.GetUser(CreateU serWizard1 .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.ExecuteScala r();
cwConn.Close();
cwConn.Dispose();
double invNum = System.Convert.ToDouble(ob jInvNum);
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)CreateUserW izard1.Cre ateUserSte p.ContentT emplateCon tainer.Fin dControl(" InsertExtr aInfo");
MembershipUser User = Membership.GetUser(CreateU serWizard1 .UserName) ;
object UserGUID = User.ProviderUserKey;
DataSource.InsertParameter s.Add("Use rId", UserGUID.ToString());
DataSource.InsertParameter s.Add("Dat eRegistere d", DateTime.Now.ToString());
DataSource.InsertParameter s.Add("IPA ddress", Request.UserHostAddress.To String());
DataSource.InsertParameter s.Add("Act iveUser", flagCCGood == true ? "1" : "0" );
string invoiceNumChk = Convert.ToString(InvoiceNu m()); // works fine, returns the value of InvoiceNum
DataSource.InsertParameter s.Add("Inv oiceNum", 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!!
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)CreateUserW
string conString = DataSource.ConnectionStrin
SqlConnection cwConn = new SqlConnection(conString);
cwConn.Open();
MembershipUser User = Membership.GetUser(CreateU
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.ExecuteScala
cwConn.Close();
cwConn.Dispose();
double invNum = System.Convert.ToDouble(ob
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)CreateUserW
MembershipUser User = Membership.GetUser(CreateU
object UserGUID = User.ProviderUserKey;
DataSource.InsertParameter
DataSource.InsertParameter
DataSource.InsertParameter
DataSource.InsertParameter
string invoiceNumChk = Convert.ToString(InvoiceNu
DataSource.InsertParameter
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!!
Why dont you use a DB sequence to do this, instead of manually doing it?
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
ASKER
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?
Does InvoiceNum() return some values?
ASKER
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
>> 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
ASKER
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)
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.CreateUs erStep... Is this a third party component?
ASKER
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:LocalSQL Server %>"
InsertCommand="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)" >
<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="SelectedValu e" />
</InsertParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="InsertExtraInfo" runat=server ConnectionString="<%$ ConnectionStrings:LocalSQL
InsertCommand="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)" >
<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"
</InsertParameters>
</asp:SqlDataSource>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Okay... well that was stupid mistake on my part... thanks for the help!