New User - Unique User ID

I have created a table to hold a userID, firstName, lastName, email, username and password.

I currently have my code to enter in the values from text boxes except for the userID.
 
            SqlDataSource1.InsertParameters("firstName").DefaultValue = txtFirstName.Text
            SqlDataSource1.InsertParameters("lastName").DefaultValue = txtLastName.Text
            SqlDataSource1.InsertParameters("email").DefaultValue = txtEmail.Text
            SqlDataSource1.InsertParameters("username").DefaultValue = txtUsername.Text
            SqlDataSource1.InsertParameters("password").DefaultValue = txtPassword.Text

 

Lets say there is a single entry in the database with the userID = 1001, how do I obtain the highest current value from userID so that I can set it to a variable like so

            intID = <code to obtain highest userID>

            SqlDataSource1.InsertParameters("userID").DefaultValue = intID
 

Help is much appreciated! 

0
kwjohns
4/19/2007 2:51:36 AM
asp.net.sql-datasource 29906 articles. 0 followers. Follow

11 Replies
702 Views

Similar Articles

[PageSpeed] 31

Oops! I meant SqlDataSource1.InsertParameters("userID").DefaultValue = intID + 1
0
kwjohns
4/19/2007 2:55:39 AM

set your userID column to an Identity column in sql server and it will correctly populate itself.

trying to write you own code to manage the incrementing id is not an easy task. your site could be hit by multiple users simultaneously causng you to generate identical userIds for different users.

 


Mike Banavige
~~~~~~~~~~~~
Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
0
mbanavige
4/19/2007 2:56:21 AM

Alright I did that. Thanks!

 

When I go to run the code I get a

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.


Source Error:

Line 44: 
Line 45: Else
Line 46: SqlDataSource1.InsertParameters("firstName").DefaultValue = txtFirstName.Text
Line 47: SqlDataSource1.InsertParameters("lastName").DefaultValue = txtLastName.Text
Line 48: SqlDataSource1.InsertParameters("email").DefaultValue = txtEmail.Tex
 

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
signup.btnSignup_Click(Object sender, ImageClickEventArgs e) in F:\Dr. Nord 4133\kuck\signup.aspx.vb:46
System.Web.UI.ImageClickEventHandler.Invoke(Object sender, ImageClickEventArgs e) +0
System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e) +105
System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +115
System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102
 
Any ideas? 

0
kwjohns
4/19/2007 3:44:27 AM

are you using SQLDataSource control? if yes have you defined insert parameters in .aspx

and SqlDataSource1.InsertParameters("firstName").DefaultValue = txtFirstName.Text should be

SqlDataSource1.InsertParameters("@firstName").DefaultValue = txtFirstName.Text

and if you've not defined at design time then you can use something like following

sqldatasource.parameters.addwithvalue("@firstname","value");

please try and let us know.

thanks,

satish.


Kind Attn: If a reply to your post helped you, kindly mark it as Answered.
__________________________________________________
Please save Animals Help World Society For Protection Of Animals,
Protect these speechless creatures of GOD
0
satish_nagdev
4/19/2007 3:49:59 AM

This is my SQLDataSource code

 

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=****;Initial Catalog=kuck;User ID=****;Password=****" ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM [users]" ConflictDetection="CompareAllValues" OldValuesParameterFormatString="original_{0}" InsertCommand="INSERT INTO [users] ([firstName], [lastName], Email, [username], [password]) VALUES (@firstName, @lastName, @email, @username, @password)">
</asp:SqlDataSource>

0
kwjohns
4/19/2007 4:07:19 AM

you are missing something like following

<InsertParameters>
    <asp:Parameter Name="EmpID" Direction="Output" Type="Int32" DefaultValue="0" />
  </InsertParameters>

read this link it may help you. http://msdn2.microsoft.com/en-us/library/z72eefad.aspx

thanks,

satish.


Kind Attn: If a reply to your post helped you, kindly mark it as Answered.
__________________________________________________
Please save Animals Help World Society For Protection Of Animals,
Protect these speechless creatures of GOD
0
satish_nagdev
4/19/2007 5:27:21 AM

It's getting close! The page doesn't give an error anymore. However, when checking the database file in SQL Server Management Studio, it didn't add the new entry. Tongue Tied

 
Here's what I have now:

 ASPX file:

  
			    <asp:ImageButton ID="btnSignup" runat="server" OnClick="btnSignup_Click" ImageUrl="images/btnsignup.jpg" />
  <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=***;Initial Catalog=***;User ID=***;Password=***" ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM [users]" ConflictDetection="CompareAllValues" OldValuesParameterFormatString="original_{0}" InsertCommand="INSERT INTO [users] ([firstName], [lastName], Email, [username], [password]) VALUES (@firstName, @lastName, @email, @username, @password)"> <InsertParameters> <asp:Parameter Name="firstName" /> <asp:Parameter Name="lastName" /> <asp:Parameter Name="email" /> <asp:Parameter Name="username" /> <asp:Parameter Name="password" /> </InsertParameters> </asp:SqlDataSource>

  

 

In the vb file: 

 

 

 	    SqlDataSource1.InsertParameters("firstName").DefaultValue = txtFirstName.Text
            SqlDataSource1.InsertParameters("lastName").DefaultValue = txtLastName.Text
            SqlDataSource1.InsertParameters("email").DefaultValue = txtEmail.Text
            SqlDataSource1.InsertParameters("username").DefaultValue = txtUsername.Text
            SqlDataSource1.InsertParameters("password").DefaultValue = txtPassword.Text
  

 

Thanks so much for the help, guys! 

0
kwjohns
4/19/2007 6:08:08 AM

did you actually called sqldatasource.insert() method ? one more sample link Big Smile >>> http://www.codeproject.com/aspnet/InsertSqlDataSource.asp

but i believe sqldatasource1.insert() should help you

thanks.

satish


Kind Attn: If a reply to your post helped you, kindly mark it as Answered.
__________________________________________________
Please save Animals Help World Society For Protection Of Animals,
Protect these speechless creatures of GOD
0
satish_nagdev
4/19/2007 6:15:44 AM

OK, that got the information to add into the database but now

1) It's adding two entries instead of just one into the database

2) userid is setup as uniqueidentifier, but isn't being given any values. Each entry is NULL. I'm guessing this is due to a setting I need to change in Management Studio Express?
 

0
kwjohns
4/19/2007 6:38:13 AM

please close this thread by marking appropriate replies as answers. the above 2 points seem to be un-related to it so you may post them as new threads(question).

this will help other users to find specific solutions. you are free to create separate posts.

we'll be glad to help you. do post your sample code

thanks,

satish.


Kind Attn: If a reply to your post helped you, kindly mark it as Answered.
__________________________________________________
Please save Animals Help World Society For Protection Of Animals,
Protect these speechless creatures of GOD
0
satish_nagdev
4/19/2007 6:47:23 AM

kwjohns:
2) userid is setup as uniqueidentifier, but isn't being given any values. Each entry is NULL. I'm guessing this is due to a setting I need to change in Management Studio Express?

you should disallow null values for your userid column.  then to get the db to generate a uniqueidentifier during inserts, you add a default value to the userid column of newid()

Personally, i prefer to keep columns like this as Identity columns of type Integer rather than using a UniqueIdentifier.

 


Mike Banavige
~~~~~~~~~~~~
Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
0
mbanavige
4/19/2007 12:38:05 PM
Reply: