Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i try query in DataGridView in C#
but this query Not Work Properly i think problem in my String
Because When is use this
C#
string DataLoad = "select cu.Date, ln.SrNumber, cu.Name+' / '+cu.FatherName AS \"Details\", cu.Address+' , '+cu.Address1 AS Address, cu.MobileNo+' , '+cu.SecMobileNo AS Contect, ln.SrNumber,  ln.PaymentDate, ln.Amount  from Payment ln inner join customer cu on ln.SrNumber=cu.SrNumber where YEAR(cu.Date) =2020 and MONTH(cu.Date)=02";


this Query Work fine

What I have tried:

C#
private void LoadDataMonth()
        {
            try
            {
                string[] Data1 = MonthlyReport.setmonth.Split('/');
                string montsPart = Data1[0];
                string yearPart = Data1[1];
                textBox2.Text = montsPart;
                textBox3.Text = yearPart;
                string DataLoad = "select cu.Date, ln.SrNumber, cu.Name+' / '+cu.FatherName AS \"Details\", cu.Address+' , '+cu.Address1 AS Address, cu.MobileNo+' , '+cu.SecMobileNo AS Contect, ln.SrNumber,  ln.PaymentDate, ln.Amount  from Payment ln inner join customer cu on ln.SrNumber=cu.SrNumber where YEAR(cu.Date) ='" + textBox3.Text + "' and MONTH(cu.Date)='" + textBox2.Text + "'";
                
                String connstring = ConfigurationManager.ConnectionStrings["Data"].ConnectionString;
                using (OleDbConnection con = new OleDbConnection(connstring))
                {
                    con.Open();
                    using (OleDbDataAdapter da = new OleDbDataAdapter(DataLoad, con))
                    {
                        DataTable dt = new DataTable();
                        da.Fill(dt);
                        dataGridView1.DataSource = dt;

                    }
                }
            }
            catch
            {
                MessageBox.Show("Data Not Found");
            }
        }
Posted
Updated 3-Jun-20 14:58pm
v3

Don't do it like that!

Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
C#
private void LoadDataMonth()
{
    try
    {
        string[] Data1 = MonthlyReport.setmonth.Split('/');
        string montsPart = Data1[0];
        string yearPart = Data1[1];
        textBox2.Text = montsPart;
        textBox3.Text = yearPart;
        
        const string query = "select cu.Date, ln.SrNumber, cu.Name+' / '+cu.FatherName AS \"Details\", cu.Address + ' , ' + cu.Address1 AS Address, cu.MobileNo + ' , ' + cu.SecMobileNo AS Contect, ln.SrNumber, ln.PaymentDate, ln.Amount from Payment ln inner join customer cu on ln.SrNumber = cu.SrNumber where YEAR(cu.Date) = @Year and MONTH(cu.Date) = @Month";
        
        string connstring = ConfigurationManager.ConnectionStrings["Data"].ConnectionString;
        using (OleDbConnection con = new OleDbConnection(connstring))
        using (OleDbDataAdapter da = new OleDbDataAdapter(query, con))
        {
            da.SelectCommand.Parameters.AddWithValue("@Year", yearPart);
            da.SelectCommand.Parameters.AddWithValue("@Month", montsPart);
            
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Data Not Found - " + ex.Message);
    }
}

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
 
Share this answer
 
Comments
Maciej Los 3-Jun-20 14:41pm    
5ed!
Amar chand123 3-Jun-20 22:03pm    
i try this code but still not Work, in my dataGridView data not show
Richard Deeming 4-Jun-20 4:49am    
Then your query isn't returning any data. We don't have access to your database, so we can't tell you why.
Amar chand123 4-Jun-20 4:58am    
But when i use Year(cu.date) = 2020 it's work fine and data show in datagridview
But when i try Year(cu.date) = @Year it's not work
Richard Deeming 4-Jun-20 4:59am    
Then your code to extract the yearPart variable is wrong.

Set a breakpoint on the first line of the method and step through it, examining the variables to see what the problem is.
print out the query you are building and run it manually to see if it is formatting right
 
Share this answer
 
Comments
Dave Kreskowiak 3-Jun-20 21:59pm    
First things first. Fix the "use parameters instead of string concatentation" problem and just about all of the OP's problem go away.

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