-3

There is a table named cartdesignsizes. It has a column cartkey. I enter cartkey in a text box and I click the button and I want the entire row against that cartkey and I want to display that row in a datagridview.

Here is the code:

 private void btncartdesign_Click(object sender, EventArgs e)
        {
            {
                SqlConnection sql = new SqlConnection("Data Source=75.119.176.76;Initial Catalog=virtual-11;Persist Security Info=True;User ID=sa;Password=kornit932");
                sql.Open();
                DataTable dt = new DataTable();
                SqlDataAdapter sd = new SqlDataAdapter("select * from CartdesignSizes where cartkey=@txtcartkey", sql);
                SqlCommand cmd = new SqlCommand("select * from CartdesignSizes where CartKey=@txtcartkey", sql);
                cmd.Parameters.AddWithValue("@txtcartkey", txtcartkey.Text);
                cmd.ExecuteNonQuery();
                sd.Fill(dt);
                dataGridView.DataSource = cmd.tables[0];
                sql.Close();
                label2.Visible = true;
                label2.Text = dataGridView.Rows.Count.ToString();

            }
        }

Code behind the text box:

DataView DV = new DataView(dbdataset);
            DV.RowFilter=String.Format("CartKey LIKE '{0}'",txtcartkey.Text);
            dataGridView.DataSource = DV;
1
  • kindly suggest me a better way of doing it
    – Aly Goreja
    Aug 29, 2015 at 13:21

1 Answer 1

1

You should change this line (I fail to see how you could compile it because the SqlCommand doesn't have a tables property

dataGridView.DataSource = cmd.tables[0];

to

dataGridView.DataSource = dt;

However your code should be changed to remove some useless duplication of code and introducing the using statement to have a better handling of the disposition of the objects involved in this query

private void btncartdesign_Click(object sender, EventArgs e)
{
    string sqlText = @"select * from CartdesignSizes 
                       where cartkey=@txtcartkey";
    DataTable dt = new DataTable();
    using(SqlConnection sql = new SqlConnection(.....))
    using(SqlDataAdapter sd = new SqlDataAdapter(sqlText, sql))
    {
        sd.SelectCommand.Parameters.Add("@txtcartkey", 
                      SqlDbType.NVarChar).Value = txtcartkey.Text;
        sd.Fill(dt);
        dataGridView.DataSource = dt;
        label2.Visible = true;
        label2.Text = dataGridView.Rows.Count.ToString();
    }
}

In this way the two disposable objects involved (the connection and the adapter) are correctly disposed at the end of the using block. No need to create a separate command because the adapter has already a property defined for the SelectCommand and you can use it to add the parameters required.

Notice also that if you use an adapter in this way you don't need to open/close the connection. This is done automatically by the adapter inside the Fill call.

1
  • Thanks a bundle Steve
    – Aly Goreja
    Aug 30, 2015 at 4:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.