locked
Error: Invalid Column Name RRS feed

  • Question

  • Hi all,
    I have written the following code in visual studio 2008:
    private void button2_Click(object sender, EventArgs e)
            {
               
                    textBox3.Visible = true;
                    textBox4.Visible = true;
                    SqlConnection con1 = new SqlConnection();
                    con1.ConnectionString = "Data Source = .; Initial Catalog = HRForADOFirstChapActivity; Integrated Security = SSPI";
                    con1.Open();
              
                  SqlCommand cmd = new SqlCommand("Select * from LoginDetails where UserName = "+textBox1.Text+"" , con1);
                    SqlDataReader read=cmd.ExecuteReader();            
                    read.Read();
                    textBox3.Text = read[0].ToString();
                    textBox4.Text = read[1].ToString();
               }

       
              In the above program I am trying to search the password of the user based on the username entered by the user.

    However When I execute the above program and enter the username as ABC and click the search button I am getting the following error:

    SQLSERVER unhandeled Exception:
    Invalid column name ABC.

    It is showing the error in the following line at the run time:
                    SqlDataReader read=cmd.ExecuteReader();            

    I am using SQLSERVER 2005 as the backend.

    Can anyone help me in finding out the error?

    What should I do to resolve the above error?

    Please help me out.

    Thanks in advance...
    Wednesday, May 20, 2009 5:37 PM

Answers

  • This thread should have been moved to the SQL board, but since it shows "unable to move", I'll try to answer your question here.

    Looks like you are missing quotation marks around UserName = 'ABC', change the string to include '' and try again.

    Regards,

    PQ

    Peter Q. http://blogs.msdn.com/peter_qian
    • Marked as answer by SNK08 Friday, July 10, 2009 4:04 AM
    Monday, June 8, 2009 9:50 PM
    Answerer