I have one table in which I have two column. I need to compare those columns value by textbox, one by one.
Eg: If user will enter user name and password then it will check in the database that it is correct or not.
Please reply for this....
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.
Amruta KirdatPosted Mar 24, 2011, 2:05 AM
chk following code
//for this i used access database and assume you have two txtboxes to take input for Name and Password resp.
oledbConnection conn = new OleDbConnection(strConn); //strConn is your connectionString
string strQuery = "Select Name,Password from Login"; //Table Name : Login with two Clmns:Name and Password
conn.open();
oledbCommand cmd = new OleDbCommand(strQuery,conn);
oledbAdapter adapter = new OleDbAdapter(cmd);
dataset ds = new dataset();
adapter.Fill(ds);
conn.close();
//declare these variables at beginning
string strName;
string strPassword;
string strNameText;
string strPassText;
strNameText = txtName.Text; //textbox from where you r taking i/p
strPassText = txtPassword.text;
if(ds.tables[0].rows.count>0)
{
for(int i=0;i
strName = convert.ToString(ds.tables[0].rows[i]["Name"].tostring()); //fetch Name clm from db
strPasword = convert.ToString(ds.tables[0].rows[i]["Password"].tostring());
if(strNameText == strName && strPassText == strPasword)
{
//correct;
}
else
{
//incorrect
}
}
}
Sahil JaniPosted Mar 25, 2011, 1:24 AM
Sahil JaniPosted Mar 25, 2011, 1:23 AM
Micke BlomqvistPosted Mar 24, 2011, 1:53 AM
CREATE PROCEDURE [dbo].[GetUserInfo]
@username varchar(10),
@pwd varchar(10)
AS BEGIN select userid, username, email from users where userid = @username and pwd = @pwd END
Then you need to invoke that procedure from your form. Put this in your click event:
try
{
using (SqlConnection c = new SqlConnection(c_string))
{
using (SqlCommand cmd = new SqlCommand("GetUserInfo", c))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UserId", value_from_textbox1);cmd.Parameters.AddWithValue("@Pwd", value_from_textbox2);
using (DataSet dsUser = new DataSet())
{
using (System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter(cmd))
{
c.Open();
da.Fill(dsUser);
c.Close();
// dsUser should contain of one row
}
}
}
}
}
catch(Exception rror) {throw new Exception("Couldn't fetch user from database. Msg: " + rror.Message);}
If you get a datarow back from the database, your user has been validated. This is very simple described, but I'll guess you get the point.