Click here to Skip to main content
15,914,444 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
String str1 = "select count(*) from UserInfo where [UserName]='" + this.CreateUserWizard1.UserName.Trim() + "'";
            OleDbConnection conn = new OleDbConnection("ASPNETDB");
            OleDbCommand cmd = new OleDbCommand(str1, conn);
            conn.Open();
            Int32 dr2 = (Int32)cmd.ExecuteScalar();
            dr2 = Convert.ToInt32(dr2);
            if (dr2 > 0)
Posted
Updated 27-Mar-13 6:21am
v4
Comments
joshrduncan2012 27-Mar-13 12:25pm    
Have you not done any google searching as to what the lines of code mean in the msdn documentation?
Richard C Bishop 27-Mar-13 12:31pm    
I answered this question due to the fact that I provided the code(some of it) being used by the OP. An explanation on my part is more beneficial than a code dump in this case I believe.
Aleu Philip 27-Mar-13 12:33pm    
thanks richb, i really appreciate. What else would i 2weeks old in c# do but learn from the experts..
Richard C Bishop 27-Mar-13 12:36pm    
You are welcome. Keep in mind that any question you may have as a newb has been answered several times and can be found with a search online.
phil.o 27-Mar-13 12:27pm    
It seems you need a C# course from scratch.

Here it is broken down line by line:

1. This line creates a string variable and assigns it a select statement value.

2. This line instantiates a new OleDbConnection.

3. This line instantiates a new OleDbCommand.

4. This line opens the connection to the database.

5. This line creates an Int32 variable and assigns it the returned value of the ExecuteScalar() method.

6. This line converts the Int32 variable to an Int32(why?).

7. This line is only a fraction of an if statement but checks whether dr2 is greater than 0.
 
Share this answer
 
ExecuteScalar execute the request on the database and returns the first answer (row and column).
In your case in should be the number of row (count(*))in UserInfo table where UserName is the username specified.

But I do not understand the need of the cast and Convert! dr2 is an Int32, there is no need to convert it to Int32.

But checking the type of result before (Int32) cast should avoid any exception.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900