0

When I use the scanner to scan the barcode,
the item will be add in the first row and when I scan the second barcode,
the item will no add in the datagridview but it just adds a row only.
My column in datagridview is productid, ProductName, Description, Stock, UOM, Price

 private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Enter)
            {
                DataGridViewRow newRow = new DataGridViewRow();
                if (textBox1.Text.Length != 0)
                {
                    conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=F:\Database\book1.mdf;Integrated Security=True;Connect Timeout=30");
                    conn.Open();
                    SqlDataAdapter adp = new SqlDataAdapter("SELECT productid,ProductName,Description,Stock,UOM,Price from ProductTable where productId='" + textBox1.Text + "'", conn);
                    DataTable dt = new DataTable();
                    adp.Fill(dt);

                    foreach (DataRow item in dt.Rows)
                    {
                        int i = dataGridView1.RowCount -1;
                        dataGridView1.Rows.Insert(i);
                        dataGridView1.Rows.Add();
                        dataGridView1.Rows[i].Cells[0].Value = item[0].ToString();
                        dataGridView1.Rows[i].Cells[1].Value = item[1].ToString();
                        dataGridView1.Rows[i].Cells[2].Value = item[2].ToString();
                        dataGridView1.Rows[i].Cells[3].Value = item[3].ToString();
                        dataGridView1.Rows[i].Cells[4].Value = item[4].ToString();
                        dataGridView1.Rows[i].Cells[5].Value = item[5].ToString();
                    }
                }
            }
        }

Page Screenshots:

LauTan
  • 23
  • 5
  • Not added in DGV but a row is added? I'm not sure to understand that part. Isn't a new row you wish? – Cid Jan 28 '19 at 12:07
  • because my datagridview have added delete button, total amount cell, so when I scan the second barcode, it will so the delete button...row only, no the data that I scan – LauTan Jan 28 '19 at 12:11

2 Answers2

0

Your approach if your productid is a unique key as it should be, will always be returning only one result, I really dont see the need of the foreach statement here. Moreover every time you open a conn to the database you should be closing it.

My approach with this in mind would be a little different this would be

Public Class clsConn
{
Public List<Data> getSomething()
    var SqlConn = new SqlConnection("your connection");
        try
        {
            SqlConn.Open();

            string sqlstring = "your sql sentence";

            SqlCommand SqlCmd = new SqlCommand(sqlstring, SqlConn);

            SqlDataReader reader = SqlCmd.ExecuteReader();

            List<Data> dataList = new List<Data>();

            if (reader.Read())
            {
                    Data data = new Data();
                    data.productid = reader[0].ToString(); // this is just an example
                    dataList.Add(data);
            }
            return dataList;
        }
        catch (Exception ex)
        {
            MessageBox.Show("conexion to DB failed: " + ex.Message);
            throw;
        }
        finally
        {
            SqlConn.Close();

        }
    }
  }
 }

And you should have a public data class that has all the properties you need like this for example

public class Data
{
    public string productid { get; set; }
}

To use it, you have to work like this

    List<Data> dbData = new List<Data>();
    clsConn db = new clsConn();
    dbData = db.getSomething();
    //I ll leave the foreach but as I said this should be only one result
    foreach (var item in DBData) 
    {
        dataGridView1.Rows.Add(item.productid);
    }
nalnpir
  • 1,167
  • 6
  • 14
0

Your .Insert()-call does not provide the row to insert, and you do not handle the index returned from the Rows.Add()-call.

I have edited your code a bit so that it should work now.

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if ((e.KeyCode != Keys.Enter) || (textBox1.Text.Length == 0))
    {
        return;
    }

    conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=F:\Database\book1.mdf;Integrated Security=True;Connect Timeout=30");
    conn.Open();
    SqlDataAdapter adp = new SqlDataAdapter("SELECT productid,ProductName,Description,Stock,UOM,Price from ProductTable where productId='" + textBox1.Text + "'", conn);
    DataTable dt = new DataTable();
    adp.Fill(dt);

    foreach (DataRow item in dt.Rows)
    {
        int i = dataGridView1.Rows.Add();
        DataGridViewRow row = dataGridView1.Rows[i];
        row.Cells[0].Value = item[0].ToString();
        row.Cells[1].Value = item[1].ToString();
        row.Cells[2].Value = item[2].ToString();
        row.Cells[3].Value = item[3].ToString();
        row.Cells[4].Value = item[4].ToString();
        row.Cells[5].Value = item[5].ToString();
    }
}

And do not forget to close your database connection. Consider using the using-statement for this.

You should also check this: How to add a new row to datagridview programmatically

Stefan Illner
  • 179
  • 1
  • 5
  • 13
  • I follow your code run but also can't run,it also can scan one only – LauTan Jan 29 '19 at 02:17
  • i just add this (textBox4.Text=" ";) after the connection close ,it show the scanner and can add the row but only can scan one digit number only,so how can i scan the many digit barcode? – LauTan Jan 29 '19 at 13:04