Prathap S V Genny
Prathap S V Genny

Reputation: 67

want to compare the text entered in textbox with the sql column, and to pull the other values from that row, if the values matches using asp.net c#

I want to compare the data entered in textbox with the column data in the SQL.if the text entered matches with the text present in the sql column, then i want to pull some values present in that row to the front end.

below Code not working :

protected void Button2_Click(object sender, EventArgs e){
      SqlDataReader reader = null;
      SqlConnection conn = new SqlConnection("Data Source=PRATHAP-GENNY\\SQLEXPRESS;Initial Catalog=Testing;Integrated Security=True");
      conn.Open();
      SqlCommand cmd = new SqlCommand("select barcodetest from Testtable where @barcodetest='" + barcode.Text + "'", conn);
      cmd.Parameters.AddWithValue("@barcodetest", barcode.Text);
      reader = cmd.ExecuteReader();
     if (reader != null && reader.HasRows)
        {
     //email exists in db do something
            TextBox2.Visible = true;

        }
     else
       {

     TextBox3.Visible = true;
        }

  }

Upvotes: 0

Views: 12860

Answers (3)

MDMalik
MDMalik

Reputation: 4001

Here are two different sets of code which I have written for you. Try copy pasting them. I believe they will work.

        SqlDataReader reader = null;
        SqlConnection conn = new SqlConnection(@"Data Source=PRATHAP-GENNY\SQLEXPRESS;Initial Catalog=Testing;Integrated Security=True");
        conn.Open();
        SqlCommand cmd = new SqlCommand("select barcodetest from Testtable where barcodetest=@barcodetest", conn);
        cmd.Parameters.AddWithValue("@barcodetest", barcode.Text);
        reader = cmd.ExecuteReader();
        if (reader != null && reader.HasRows)
        {
            //email exists in db do something
            TextBox2.Visible = true;
        }
        else
        {

            TextBox3.Visible = true;
        }

Below I have written another modified code as you do not get anything from your SQL Command. Hence I bought an extra value. Which might give an error as this column will not be present in your table.

Just to give you an idea how to read those values

 // My MODIFIED CODE WOULD BE
        SqlDataReader reader = null;
        SqlConnection conn = new SqlConnection(@"Data Source=PRATHAP-GENNY\SQLEXPRESS;Initial Catalog=Testing;Integrated Security=True");
        conn.Open();
        SqlCommand cmd = new SqlCommand("select productname, barcodetest from Testtable where barcodetest=@barcodetest", conn);
        cmd.Parameters.AddWithValue("@barcodetest", barcode.Text);
        reader = cmd.ExecuteReader();
        while (reader.Read())
        { 
            textBox1.Visible= true;
            textBox1.Text = Convert.ToString(reader["productname"]);

            textBox2.Visible = true;
            textBox2.Text = Convert.ToString(reader["barcodetest"]);
        }
        else
        {

            textBox1.Visible = textBox2.Visible= false;
        }

If there is any confusion do feel free to ask me.

Upvotes: 4

James Kyburz
James Kyburz

Reputation: 14453

Try changing line

SqlCommand cmd = new SqlCommand("select barcodetest from Testtable where @barcodetest='" + barcode.Text + "'", conn);

to

SqlCommand cmd = new SqlCommand("select barcodetest from Testtable where barcodetest=@barcodetest"), conn);

Upvotes: 2

Gambrinus
Gambrinus

Reputation: 2136

well you have everything you need - change your select statement to * or add all the columns you would need later on - so you could now enter your values into the textbox2 or textbox3 using:

 TextBox2.Text = String.Format("Value: {0}", reader["barcodetest"]);

or via index

TextBox2.Text = String.Format("Value: {0}", reader[0]);

see documentation of sqldatareader

Upvotes: 0

Related Questions