I have what I am sure is a simple question. I've been searching online for an hour and have $180 worth of VB.NET books in front of me but I can't seem to find this. I have a form where end users create an account/membership that is stored to a SQL DB. The username
fields have to be unique and I can't figure out how to query that info from the DB? I would just like to check if there are any users currently using that username and if so, just generate a textbox message asking them to create a different username. Thanks!
Building websites for 7 years. Using VB.NET for 2 weeks....
'amatuer'
If the username is a primary key or a unique key and you try to insert a duplicate key, the data provider will throw an exception. In other words, you can wrap the account creation code in a try-catch block and if an exception is thrown then display in a label
that the username is already taken. Of course, you may want to inspect the exception thrown to see that it really was thrown as a result of a duplicate key.
Ok I really appreciate the help but that just blew right over my head. I understand what you are saying perfectly but I do not understand the "lingo" your using as I am a begginer. I also understand that it's in your best interest to lead me down the path and
let me figure it out without actually giving me the code, and that's the way I want to do it otherwise I won't learn anything other than how to cut and paste. Is there anyway you could explain that in laymans terms? My thought is that it's going to be something
like:
if username <> Table.username then
'run the application
else label1.Text = "That username is already in use"
As you can see I am a newbie but I think I have the general idea? I just can't find the right language.
Building websites for 7 years. Using VB.NET for 2 weeks....
'amatuer'
>to lead me down the path and let me figure it out without actually giving me the code, and that's the way I want to do it otherwise I won't learn anything other than how to cut and paste I really appreciate your attitude. :) That mindset will get you
a lot further than you think. Anyway, to explain further, if you have a unique field in the a table (e.g. a primary key), the database will not let you insert duplicate keys. For instance, if my user table already has a username value of "stanley" and I try
to insert another user of value "stanley" the database will complain with an error. From a .NET application, this will result in an exception. Hence, what I can do is wrap the code to insert the user into the table in a try-catch block and if an exception
is thrown then you know that the user already exists. I'm not a VB programmer, but here goes nothing:
Try
' code to insert user into database
Catch e As Exception
' the user already exists
Label1.Text = "The username is already in use."
End Try
If that doesn't make sense, just tell me what part I need to elaborate on. ;)
That totally makes sense. Thank you! So do I need to change the 'userName' row type in the SQL DB from 'varchar' to 'uniqueindentifier'? Actually I just learned about the "Try" function while trying to learn how to upload photo's ... which is turning out to
be extremley difficult, but I'm hanging in there :-|
Building websites for 7 years. Using VB.NET for 2 weeks....
'amatuer'
I believe the data type 'uniqueindentifier' will only accept guid like values i.e. {08b0b2da-3fb3-11d3-a4de-00c04f610189}; you DO want your UserName column to be varchar, but you want to add a unique constraint to it, this is not nessesarly the primary key
of the record. Maybe this will help?
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/createdb/cm_8_des_04_0bqr.asp I personally do not like the try catch for determining if the requested login is alreay taken. maybe
// untested sudo c# code
SqlConnection connection = new SqlConnection("some db connection");
string sRequestedUserName = Request.Form["RequestedUserName"];
// use a stored procedure instead.
string sql = "SELECT COUNT(UserID) FROM User WHERE UserName = '" + sRequestedUserName + "';";
SqlCommand cmd = new SqlCommand(connection, sql);
int count = (int) cmd.ExecuteScaler(); // this may be incorrect, but you get the idea
if (count > 0)
' no way
else
' ok
Feeling STUPID!!! All I had to do was right click the 'userName' column and designate it as the PRIMARY KEY. I then used the 'Catch ex As Exception' because I'm really not familiar with stored procedures. Seems to be working. Thanks so much for you guys' help!
Building websites for 7 years. Using VB.NET for 2 weeks....
'amatuer'
Vision_619
Member
115 Points
23 Posts
Simple Question ... Please help!
Nov 02, 2003 04:34 AM|LINK
'amatuer'
Stanley Tan
Contributor
2035 Points
405 Posts
Re: Simple Question ... Please help!
Nov 02, 2003 04:56 AM|LINK
theSpoke Blog
Vision_619
Member
115 Points
23 Posts
Re: Simple Question ... Please help!
Nov 02, 2003 05:21 AM|LINK
'amatuer'
Stanley Tan
Contributor
2035 Points
405 Posts
Re: Simple Question ... Please help!
Nov 02, 2003 10:46 AM|LINK
Try ' code to insert user into database Catch e As Exception ' the user already exists Label1.Text = "The username is already in use." End TryIf that doesn't make sense, just tell me what part I need to elaborate on. ;)theSpoke Blog
Vision_619
Member
115 Points
23 Posts
Re: Simple Question ... Please help!
Nov 02, 2003 07:39 PM|LINK
'amatuer'
jhouse
Participant
1856 Points
374 Posts
Re: Simple Question ... Please help!
Nov 02, 2003 11:46 PM|LINK
// untested sudo c# code SqlConnection connection = new SqlConnection("some db connection"); string sRequestedUserName = Request.Form["RequestedUserName"]; // use a stored procedure instead. string sql = "SELECT COUNT(UserID) FROM User WHERE UserName = '" + sRequestedUserName + "';"; SqlCommand cmd = new SqlCommand(connection, sql); int count = (int) cmd.ExecuteScaler(); // this may be incorrect, but you get the idea if (count > 0) ' no way else ' okVision_619
Member
115 Points
23 Posts
Re: Simple Question ... Please help!
Nov 03, 2003 12:09 AM|LINK
'amatuer'