Add row in datagrid after select from sql query
First time when select is empty row, second is full row
Some help please
Need when select from db value to insert in datagrid
Query is ok
Problem is from line 22 to line 33
- private void prijavaAction()
- {
- int sno = dataGridView1.Rows.Count + 1;
- SqlConnection con = new SqlConnection(cs);
- if (textBox1.Text.All(char.IsDigit))
- {
- string queryString = "select bar_kod, ime, cijena_sa_porezom from roba_usluge WHERE bar_kod = '" + textBox1.Text + "'";
- using (SqlConnection connection = new SqlConnection(cs))
- {
- SqlCommand command = new SqlCommand(queryString, connection);
- connection.Open();
- SqlDataReader reader = command.ExecuteReader();
- if (reader.Read())
- {
- for (int i = 1; i < dataGridView1.Rows.Count; i++)
- {
- dataGridView1.Rows[i].Cells["redni_broj"].Value = i;
- dataGridView1.Rows[i].Cells["bar_kod"].Value = reader["bar_kod"].ToString();
- dataGridView1.Rows[i].Cells["naziv_artikla"].Value = reader["ime"].ToString();
- dataGridView1.Rows[i].Cells["cijena"].Value = reader["cijena_sa_porezom"].ToString();
- }
-
- dataGridView1.Rows.Add();
- reader.Close();
- }
- else
- {
- MessageBox.Show("Bar kod ne postoji u bazi!", "Obavještenje", MessageBoxButtons.OK, MessageBoxIcon.Information);
- textBox1.Text = "";
- }
- }
- }
- else
- {
- MessageBox.Show("Bar kod ne postoji ili nije bar kod!", "Obavještenje", MessageBoxButtons.OK, MessageBoxIcon.Information);
- textBox1.Text = "";
- }
- }