Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Help me with this problem, my code is as following:

What I have tried:

I try this
private void prijavaAction() //04.04. final doradjena perfect 
{ 

{ 
SqlConnection con = new SqlConnection(cs); 

if (textBox1.Text.All(char.IsDigit)) 

{ 
string queryString = "select bar_kod, ime,pakovanje, porez_procenat,cijena_sa_porezom from roba_usluge WHERE bar_kod = '" + textBox1.Text + "'";// pronaci radnika u bazi 
using (SqlConnection connection = new SqlConnection(cs)) 
{ 
SqlCommand command = new SqlCommand(queryString, connection); 
connection.Open(); 
SqlDataReader reader = command.ExecuteReader(); 


if (reader.Read()) 

{ 
//insert into datagrid1 
} 
else 
{ 

MessageBox.Show("Bar kod not exist!", "Obavještenje", MessageBoxButtons.OK, MessageBoxIcon.Information); 
textBox1.Text = ""; 
} 
} 

}
Posted
Updated 19-Oct-18 19:12pm
v2
Comments
Goran Bibic 19-Oct-18 16:32pm    
I try thi

private void prijavaAction() //04.04. final doradjena perfect
{

{
SqlConnection con = new SqlConnection(cs);

if (textBox1.Text.All(char.IsDigit))

{
string queryString = "select bar_kod, ime,pakovanje, porez_procenat,cijena_sa_porezom from roba_usluge WHERE bar_kod = '" + textBox1.Text + "'";// pronaci radnika u bazi
using (SqlConnection connection = new SqlConnection(cs))
{
SqlCommand command = new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();


if (reader.Read())

{
//insert into datagrid1
}
else
{

MessageBox.Show("Bar kod not exist!", "Obavještenje", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.Text = "";
}
}

}
Afzaal Ahmad Zeeshan 19-Oct-18 16:51pm    
And this doesn't work?
[no name] 19-Oct-18 16:54pm    
Sorry Goran, this time I flagged your question as "not a question" and btw., I was not the first which did this....
Bryian Tan 19-Oct-18 21:21pm    
Why SqlDataReader and not SqlDataAdapter?
SqlDataAdapter da=new SqlDataAdapter(command); 
        DataTable dt=new DataTable();
        da.Fill(dt);

 if (dt.Rows.Count > 0)
        {
            dataGridView1.DataSource = dt;
            dataGridView1.DataBind();
        }
else {
   MessageBox.Show("Bar kod ne postoji!", "Obavještenje", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.Text = "";

}

That is the first thing any database tutorial would normally do; show how to render the table data in a grid.

Follow along the previous question, Display data form sql db onto datagridview with labled columns[^] , and start with the "Related Questions", in the right side of this question as well. There are several other questions that answer the same thing.

This quick article might also help you, Walkthrough: Display Data from a SQL Server Database in a DataGrid Control | Microsoft Docs[^]

Edit
Provided the code that you have tried, as well as the comment that you made. You want to make sure that the record exists, for the data grid view and only render the records it there are any.
In that case, query is correct and will most likely fetch the data if the column names and the table and connection is correct, also the code is exposed to SQL Injection. I just wrote an answer here, Sql injection attacks possible using resource(.resx) file[^] , your code will most likely.

The code for the block of reader.Read() is already shared several times in the articles and the links I have shared in my answer above.
 
Share this answer
 
v2
Comments
Goran Bibic 19-Oct-18 16:57pm    
Not display in datagrid...need to check ,,WHERE bar_kod = '" + textBox1.Text,,

if have to insert in datagrid
Some help?
Afzaal Ahmad Zeeshan 19-Oct-18 17:08pm    
Then why you need to use SQL query in the C# code? You can write a stored procedure that checks whether the bar code is correct or not. Of course it requires that you show some code that you are having.

Also, concatenation is a bad practice in SQL, it will lead to SQL Injection. I just wrote an answer on that here, https://www.codeproject.com/Answers/1264075/Sql-injection-attacks-possible-using-resource-resx
Goran Bibic 19-Oct-18 17:10pm    
Yes, if is bar_code ok, insert values in datagrid1

I try this, just read, no insert in datagreid1

private void prijavaAction()
{

{
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 + "'";// pronaci radnika u bazi
using (SqlConnection connection = new SqlConnection(cs))
{
SqlCommand command = new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();


if (reader.Read())

{
for (int i = 0; i < dataGridView1.Rows.Count; 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();
}



reader.Close();


}
else
{
MessageBox.Show("Bar kod ne postoji!", "Obavještenje", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.Text = "";


}
}

}

else
{
MessageBox.Show("Bar kod ne postoji!", "Obavještenje", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.Text = "";
}





}
}
Afzaal Ahmad Zeeshan 19-Oct-18 17:28pm    
And what is the problem with this?
Goran Bibic 19-Oct-18 17:07pm    
I try this but just read...no insert in datagridviuew1

private void prijavaAction() //04.04. final doradjena perfect
{

{
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 + "'";// pronaci radnika u bazi
using (SqlConnection connection = new SqlConnection(cs))
{
SqlCommand command = new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();


if (reader.Read())

{
for (int i = 0; i < dataGridView1.Rows.Count; 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();
}



reader.Close();


}
else
{
MessageBox.Show("Bar kod ne postoji!", "Obavještenje", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.Text = "";


}
}

}

else
{
MessageBox.Show("Bar kod ne postoji!", "Obavještenje", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.Text = "";
}





}
}
C#
GLOBALVARS.dt = new DataTable();

           if (GLOBALVARS.dt.Columns.Count == 0)
           {
               GLOBALVARS.dt.Columns.Add("bar_kod");
               GLOBALVARS.dt.Columns.Add("ime");
               GLOBALVARS.dt.Columns.Add("cijena_sa_porezom");

           }

  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();

                   ;
                   while (reader.Read())
                   {
                       if (reader.HasRows)
                       {
                           DataRow dr = GLOBALVARS.dt.NewRow();
                           dr["bar_kod"] = reader["bar_kod"].ToString();
                           dr["ime"] = reader["ime"].ToString();
                           dr["cijena_sa_porezom"] = reader["cijena_sa_porezom"].ToString();

                           GLOBALVARS.dt.Rows.Add(dr);

                           dataGridView1.DataSource = GLOBALVARS.dt;
                       }

                   }

                   if (GLOBALVARS.dt.Rows.Count == 0)
                   {
                       MessageBox.Show("Bar kod not exist!", "Obavještenje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                       textBox1.Text = "";

                   }
               }


           }
 
Share this answer
 
Comments
Goran Bibic 20-Oct-18 9:02am    
What is GLOBALVARS? Error...how to define that?
[no name] 20-Oct-18 11:30am    
public static class GLOBALVARS
{
public static DataTable dt;

}
Goran Bibic 21-Oct-18 7:55am    
Its work, just one simple proble. Just one row add in datagrid. If have two thing have just one row and change things?

Need if have 5 things, to be 5 rows. If have thing just ++ quatity. Hope to understand?
Goran Bibic 21-Oct-18 8:11am    
Second question, I have existing datagrid with values bar_kod, ime, cijena_sa_porezom.
Why this?

GLOBALVARS.dt = new DataTable();

if (GLOBALVARS.dt.Columns.Count == 0)
{
GLOBALVARS.dt.Columns.Add("bar_kod");
GLOBALVARS.dt.Columns.Add("ime");
GLOBALVARS.dt.Columns.Add("cijena_sa_porezom");

}
[no name] 22-Oct-18 11:36am    
It is needed while datatable is null.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900