C# .NET - how to check username exists in database using c#.net window application

Asked By sanjay
09-Mar-10 08:27 AM
end of post
  F Cali replied to sanjay
09-Mar-10 08:37 AM
If you are using SQL Server as your database, you can do the following:

SqlConnection sqlConnection = new SqlConnection("YourConnectionString");
SqlCommand sqlCommand = ("", sqlConnection);
int userCount;

sqlCommand.CommandType = CommandType.Text;
sqlCommand.CommandText = "SELECT COUNT(*) FROM [dbo].[YourUsersTable] WHERE [UserName] = @UserName";
sqlCommand.Parameters.Add("@UserName", SqlDbType.VarChar, 20);
sqlCommand.Parameters["@UserName"] = YourUserNameHere;

sqlConnection.Open();
userCount = Convert.ToInt32(sqlCommand.ExecuteScalar());
if (userCount != 0)
   // user name exists;
sqlConnection.Close();

Regards,
http://www.sql-server-helper.com/faq/dates-p01.aspx
  Super Man replied to sanjay
09-Mar-10 08:38 AM
   

            SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename='|DataDirectory|\Database.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True");


SqlCommand cmd = new SqlCommand("SELECT * FROM user where usernam="+ txtusername.Text, cn);


          cn.Open();

 

          

            int i=cmd.ExecuteNonQuery();

            if (i > 0)

            {

                MessageBox.Show("user with this name exists");

            }

            else

            {

                MessageBox.Show("user doesnot exists");

            }

  Sagar P replied to sanjay
09-Mar-10 08:41 AM
Use command object for that and use following query.
Query -> "select count(*) from database where userid='Given_name'" and store it into an integer.,,
Use ExecuteScalar() with that command;

if(val>0) then
 userpresent
else
 no;

Create New Account
help
Best way to coordinate native date format between client and server? SQL Server Hello, I'm running into an issue in a VB application that uses SQL Server as a back end. I have a stored procedure that uses a From and To best practice" when it comes to syncronizing the regional settings between the Client and the SErver? Thanks! Rick SQL Server Programming Discussions SQL Server 2000 (1) SQL Server 2005 (1) SQL Server (1) SqlDbType.DateTime
space planning for SQL Server SQL Server Hi I am looking for a script that will enumerate all the DBs on a sql server, show me the used MB and the max MB allocated per db. My intent would isn’t returning anything on it. Can someone aim me in the right direction? thanks SQL Server Discussions SQL Server (1) Exec (1) Disk (1) SqlDataAdapter (1) Database (1) AdventureWorks (1) SqlParameter
Why_can’t_recursive_queries_contain. . .? SQL Server hi I hope I didn = 92t put too many questions 1) Why can = 92t recursive queries also be unioned together with UNION operator ( instead they must use UNION ALL )? thanx SQL Server Programming Discussions SQL Server 2008 (1) SQL Server 2005 (1) SQL Server 2000 (1) SQL Server (1) Oracle (1) Ruby (1) MichaelcoAToptonlineDOTnet (1
Import Data from Excel file to SQL server how to import data from excel file into the SQL server Use DTS or SSIS You can use the SQL Server Data Transformation Services (DTS) Import Wizard or the SQL Server Import and Export Wizard to import Excel data into SQL Server tables. Check this link
store image in sql server 2008 can any one tell me the code for storing images in sql server 2008 with c# thanks in advance To Save images to SQL Server 2005 DB http: / / www.aspsnippets.com / post / 2009 / 02 / 19 / Save-and-Retrieve-Files-from SQL-Server-Database-using-ASPNet.aspx To retreive and display http: / / www.aspsnippets.com / post / 2009 / 02