Click here to Skip to main content
14,664,507 members
Rate this:
Please Sign up or sign in to vote.
See more:
Some help
 
In line 10 setup IsLetterOrDigit..
 
When add some value in datagrid
 
 Line 34-39 have problem...
 
Every time add new row, if put number  it ok kolicina (quantity go up +1), if is bar code type vm1403 add new row...not kolicina (quantity) +1...why? Some help?

    int cqty = Convert.ToInt32(row.Cells["kolicina"].Value) + 1;  
      
                              if (textBox1.Text == row.Cells["bar_kod"].Value.ToString())  
                              {  
                                  row.Cells["kolicina"].Value = cqty;  


What I have tried:

private void prijavaAction()  
      {  
          PullData();  
                
                
          int sno = dataGridView1.Rows.Count + 1;  
  
          SqlConnection con2 = new SqlConnection(con);  
  
          if (textBox1.Text.All(char.IsLetterOrDigit))  
  
          {  
              string queryString = "select [bar_kod], [naziv], [cijena_sa_porezom] from dbo.roba_usluge WHERE [bar_kod] = '" + textBox1.Text + "'";// pronaci artikl u bazi                
              using (SqlConnection connection = new SqlConnection(con))  
              {  
                  SqlCommand command = new SqlCommand(queryString, connection);  
                  connection.Open();  
                  SqlDataReader reader = command.ExecuteReader();  
  
  ///////////////////////////////////////////

here is error
                  var itemAndPrice = textBox1.Text;  
  
                  var SingleRow = (from Rows in dataGridView1.Rows.Cast<DataGridViewRow>() where !Rows.IsNewRow && Rows.Cells["bar_kod"].Value.ToString().ToUpper() == textBox1.Text.ToUpper() select Rows).FirstOrDefault();  
  
  
                  if (reader.Read())  
                  {  
  
                      bool barcodeexist = false;  
                      foreach (DataGridViewRow row in dataGridView1.Rows)  
                      {  
                          int cqty = Convert.ToInt32(row.Cells["kolicina"].Value) + 1;  
  
                          if (textBox1.Text == row.Cells["bar_kod"].Value.ToString())  
                          {  
                              row.Cells["kolicina"].Value = cqty;  
   ///////////////////////////////////////////


                              foreach (DataGridViewRow g1 in dataGridView1.Rows)  
                              {  
  
  
                                  g1.Cells["ukupno"].Value = (Convert.ToDouble(g1.Cells["kolicina"].Value) * Convert.ToDouble(g1.Cells["cijena"].Value)).ToString("0.00");  
  
                              }  
Posted
Updated 21-Feb-19 9:32am
Comments
Richard MacCutchan 22-Feb-19 4:31am
   
Your call to char.IsLetterOrDigit does not match the documentation (Char.IsLetterOrDigit Method (System) | Microsoft Docs[^]). In fact that code should not even compile.
Goran Bibic 22-Feb-19 7:22am
   
What to chose? When have situation:
bar_code 123456 IsDigit (this is ok and work quatity + 1)
bar_code VM123456 IsLetterOrDigit (this add new row with same item and quatity 1)

Some recomandition?
Richard MacCutchan 22-Feb-19 7:45am
   
You need to parse your data properly. Separate the digits from the letters, increment the number, and then recreate the string for the next time. It is no good trying to guess what your data looks like, you need to cater for every combination of letters and digits that may be presented.
Richard MacCutchan 22-Feb-19 7:49am
   
Your call to char.IsLetterOrDigit will always return True if the data contains all letters or digits. So you need a different method of determining the exact format of the string.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

string queryString = "select [bar_kod], [naziv], [cijena_sa_porezom] from dbo.roba_usluge WHERE [bar_kod] = '" + textBox1.Text + "'";

Not necessary a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
Quote:
Every time add new row, if put number it ok kolicina (quantity go up +1), if is bar code type vm1403 add new row...not kolicina (quantity) +1...why? Some help?

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   
Comments
Richard Deeming 22-Feb-19 11:17am
   
I've lost count of the number of times we've explained SQL Injection to the OP. Every time he(?) claims to understand, then comes back with another question demonstrating that he's completely ignored the warning.

I don't know whether it's a language barrier, a "scared by technical jargon" barrier, or just a "couldn't give a shiny shite" barrier.
Patrice T 22-Feb-19 12:19pm
   
Looks like the OP easy way: we do the search :)

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100