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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amruta Kirdat72 reputationPosted Mar 23, 2011, 10:05 PM EDT
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
}
}
}
Micke BlomqvistRank #1,660 · 923 reputationPosted Mar 23, 2011, 9:53 PM EDT
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.
Sahil Jani196 reputationPosted Mar 24, 2011, 9:23 PM EDT
Sahil Jani196 reputationPosted Mar 24, 2011, 9:24 PM EDT