Click here to Skip to main content
15,916,693 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i m trying to read as....

string a;
cmd = new SqlCommand("select role from temp where (emp_id='" + txtUserId.Text + "' and paswrd='" + txtPwd.Text + "')",cn);
                    a = cmd.ExecuteReader().Read().ToString();
                    Console.Write(a);



.... it is not giving any error ... but no value is coming in "a"....
pls help
Posted
Updated 16-Feb-12 20:47pm
v2

Try:
C#
cmd = new SqlCommand("select role from temp where (emp_id='" + txtUserId.Text + "' and paswrd='" + txtPwd.Text + "')",cn);
SqlDataReader reader = cmd.ExecuteReader().Read().ToString();
if (reader.Read())
   {
   Console.Write((string) reader["role"]);
   }
Or, better, use parametrised queries and stop people destroying your database with SQL Injection:
C#
cmd = new SqlCommand("select role from temp where (emp_id=@ID and paswrd=@PW)",cn);
cmd.Parameteres.AddWithValue("@ID", txtUserId.Text);
cmd.Parameteres.AddWithValue("@PW", txtPwd.Text);
SqlDataReader reader = cmd.ExecuteReader().Read().ToString();
if (reader.Read())
   {
   Console.Write((string) reader["role"]);
   }
Even better, don't ever store passwords in straight text! There is a Tip here that explains that: Password Storage: How to do it.[^]


"can u provaide the solution through SqlDataAdaptor..

thanks"



What did your last slave die of?
C#
using (SqlConnection connection = new SqlConnection(strConnect))
    {
    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = new SqlCommand("SELECT role FROM temp WHERE emp_id=@ID AND paswrd=@PW", connection);
    da.SelectCommand.Parameters.AddWithValue("@ID", txtUserId.Text);
    da.SelectCommand.Parameters.AddWithValue("@PW", txtPwd.Text);
    DataTable dt = new DataTable();
    da.Fill(dt);
    }
 
Share this answer
 
v2
Comments
shweta9999 17-Feb-12 4:59am    
can u provaide the solution through SqlDataAdaptor..

thanks
OriginalGriff 17-Feb-12 6:40am    
Answer updated
Varun Sareen 17-Feb-12 11:22am    
My 4+ good explained answer
shweta9999 19-Feb-12 22:05pm    
hi varun,

i want the value of "role" from the database. and i want to put the if condition... that
if(role==1)
{
responce.redirect("abc.aspx");

}
else if(role==0)
{
responce.redirect("xyz.aspx");
}

... so my question is how can i retrive the value of role field from the table using SqlDataAdaptor

thankyou..
Please try this:-

string a;
cmd = new SqlCommand("select role from temp where (emp_id='" + txtUserId.Text + "' and paswrd='" + txtPwd.Text + "')",cn);
a = cmd.ExecuteScalar().ToString();
Console.Write(a);
 
Share this answer
 
Comments
shweta9999 17-Feb-12 5:02am    
hi varun can u please provide solution using ..."sqlDataAdaptor"

thanks
Varun Sareen 17-Feb-12 11:21am    
Dear Shweta, OriginalGriff has updated his answer. Please see
shweta9999 19-Feb-12 22:24pm    
it is not working...
it says...

Object reference not set to an instance of an object.


:-(
Varun Sareen 20-Feb-12 11:30am    
ohh!!! will provide you solution soon
 
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