hi
i want to get the code for searching a record in a sql db from a VB form using ADO.NET
i want to use it for creating a username password program as a part of my project
pls help me
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
PHANI NADIGADDAPosted Nov 21, 2006, 10:46 AM
You can also perform this operation like....
private bool AuthenticateUser(string userName, string password)
{
SqlConnection connection = new SqlConnection();
try
{
connection.ConnectionString="Data Source=SQLServerName;Initial Catalog=Databasename;user=sa;password=sa"
connection.Open();
string SQLStatement="SELECT COUNT(*) FROM Login Where UserName='"+ userName +"' AND password='"+ password +"'";
SqlCommand command = new SqlCommand(SQLStatement,cnDB);
int count = command.ExcecuteScalar();
if(count == 1)
{
return true;
}
else
{
return false;
}
}
catch(Exception ex)
{
throw new Exception("Error occured while processing the request. Error Message is :" + ex.Message);
}
finally
{
connection.Close();
}
}
----------------------
try
{
if(AuthenticateUser(userName, password)
{
lblResult.Text = "Valid User Name and Password";
}
else
{
lblResult.Text = "Invalid User Name and password";
}
catch(Exception ex)
{
lblErrorMessage.Text = ex.Message.ToString();
}
ALL THE BEST.
PHANI NADIGADDAPosted Nov 21, 2006, 10:41 AM
private bool AuthenticateUser(string userName, string password)
{
SqlConnection connection = new SqlConnection();
try
{
connection.ConnectionString="Data Source=SQLServerName;Initial Catalog=Databasename;user=sa;password=sa"
connection.Open();
string SQLStatement="SELECT COUNT(*) FROM Login Where UserName='"+ userName +"' AND password='"+ password +"'";
SqlCommand command = new SqlCommand(SQLStatement,cnDB);
int count = command.ExcecuteScalar();
if(count == 1)
{
}
SqlDataReader sqlDR = cmdLogin.ExecuteReader();
int result=0;
while (sqlDR.Read())
{
Session["UserName"]=txtUser.Text;
result=1;
break;
}
sqlDR.Close();
if(result==0)
{
Label1.Text="You have entered invalid UserName/Password. Please try again. ";
}
else
{
Label1.Text="Welome";
}
}
catch(Exception ex)
{
Label1.Text= "Error occured while processing the request. Error Message is :" + ex.Message;
}
finally
{
try
{
cnDB.Close();
cnDB.Dispose();
}
catch(Exception exp)
{
}
}
}
Anand ThakurPosted Nov 21, 2006, 2:14 AM
In C# code is as follows, Convert it to VB
private void Submit1_ServerClick(object sender, System.EventArgs e)
{
SqlConnection cnDB = new SqlConnection();
try
{
cnDB.ConnectionString="Data Source=SQLServerName;Initial Catalog=Databasename;user=sa;password=sa"
cnDB.Open();
string SQLStatement="SELECT UserName,Password FROM Login Where UserName='"+ txtUser.Text +"' AND password='"+ txtPass.Text +"'";
SqlCommand cmdLogin = new SqlCommand(SQLStatement,cnDB);
SqlDataReader sqlDR = cmdLogin.ExecuteReader();
int result=0;
while (sqlDR.Read())
{
Session["UserName"]=txtUser.Text;
result=1;
break;
}
sqlDR.Close();
if(result==0)
{
Label1.Text="You have entered invalid UserName/Password. Please try again. ";
}
else
{
Label1.Text="Welome";
}
}
catch(Exception ex)
{
Label1.Text= "Error occured while processing the request. Error Message is :" + ex.Message;
}
finally
{
try
{
cnDB.Close();
cnDB.Dispose();
}
catch(Exception exp)
{
}
}
}
Enjoy......