Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void Button1_Click(object sender, EventArgs e)
    {
               OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
        try
        {
            con.Open();
        }
        catch (Exception)
        {
            
            throw;
        } 
                      string str = TextBox1.Text.ToString();
        OleDbCommand cmd = new OleDbCommand("SELECT Profession FROM name WHERE Name="+str, con);

        OleDbDataReader rdr = cmd.ExecuteReader();
        if (rdr.Read())
        {
            // string str = rdr.GetValue(1).ToString();
            TextBox3.Text = rdr["Profession"].ToString();
// TextBox3.Text = rdr.GetValue(2).ToString();
        }
        con.Close();
       
    }

this error at executereader??
Posted
Updated 3-Aug-12 1:11am
v2
Comments
dare2dash 3-Aug-12 7:10am    
thanx ....it worked for button but why same code is not working for dropdownlist>
protected void DropDownList_SelectedIndexChanged(object sender,EventArgs e){}

try this and let me know still it didnt work. You are missing singlequotes inthe query
C#
OleDbCommand cmd = new OleDbCommand("SELECT Profession FROM name WHERE Name='"+str +"'", con);
 
Share this answer
 
Comments
dare2dash 3-Aug-12 7:11am    
why the same code not working for dropdown
protected void DropDownList_SelectedIndexChanged(object sender,EventArgs e)
{}
Santhosh Kumar Jayaraman 3-Aug-12 7:43am    
post your complete code, so that we can help u
Shouldn't the SQL statement string be
SQL
"SELECT Profession FROM name WHERE Name='"+str+ "'"

?
 
Share this answer
 
Insert quotes in your command

C#
OleDbCommand cmd = new OleDbCommand("SELECT Profession FROM name WHERE Name='"+str+"'", con);
 
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