Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi Guys am working on a c# project and in this project, I want to select some data and show in Datagridview but I have data in my DB and I have a query of select but when I click the button so I get this type of error this no row at position 0 please help me guys I just need to deliver this project ASAP,
Thanks.
here are the screenshots https://imgur.com/PKvcgY9
https://imgur.com/aeq0weF
https://imgur.com/8AG3LZ4


What I have tried:

C#
dataGridView4.Columns.Add("", "Leave Consumed");
                dataGridView4.Columns.Add("", "Leave Allaowed");
                dataGridView4.Columns.Add("", "Balance");
                for (int i = 0; i < dataGridView4.Rows.Count; i++)
                {
                    DataSet ds1211122 = new DataSet();
                    DataTable dt1211122 = new DataTable();
                    ds1211122.Tables.Add(dt1211122);
                    OleDbDataAdapter da1211122 = new OleDbDataAdapter();
                    da1211122 = new OleDbDataAdapter("SELECT Sum(USERID) FROM [CHECKINOUT] where [CHECKTYPE]= '" + "I" + "'AND [USERID] = " + dataGridView4.Rows[0].Cells[1].Value.ToString() + "AND [CHECKTIME] Between #" + dateTimePicker2.Value.ToString() + "# AND #" + dateTimePicker1.Value.ToString() + "# Group By [USERID];", VCON);
                    da1211122.Fill(dt1211122);
                    int num = 0;
                    num = Convert.ToInt32(dt1211122.Rows[0][0].ToString());
                    VCON.Close();
                }
Posted
Updated 6-Mar-19 15:19pm
v2
Comments
PIEBALDconsult 6-Mar-19 20:21pm    
I think you're mixing C# and VB.

1 solution

Quote:
Error: no row at position 0

Row 0 os the first record of answer of select, what happen when answer is empty?
C#
da1211122 = new OleDbDataAdapter("SELECT Sum(USERID) FROM [CHECKINOUT] where [CHECKTYPE]= '" + "I" + "'AND [USERID] = " + dataGridView4.Rows[0].Cells[1].Value.ToString() + "AND [CHECKTIME] Between #" + dateTimePicker2.Value.ToString() + "# AND #" + dateTimePicker1.Value.ToString() + "# Group By [USERID];", VCON);

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[^]
 
Share this answer
 

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