Click here to Skip to main content
15,891,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i try show SqlDatabase data in DataGridView in C#, but when i try Code Run without Error and Data Not Show in DataGridView
if i use this in SQL Query DataShow But when i show in C# WinForm Data not showing
C#
string QueryEMI = "select ST.SrNumber, ST.StudentName+' / '+ST.FatherName AS StudentDetails, ST.Address+' , '+ST.Address1 AS Address, ST.MobileNo+' , '+ST.SecMobileNo AS Contect,  FORMAT(FD.FeesDate,'dd/MM/yyyy'), FD.Amount  from FeesDetails FD inner join Student ST on FD.SrNumber=ST.SrNumber where FD.PaymentStatus ='DUE'"


and i use this in SQL Query or C# WinForm Data not Showing in Both
C#
string QueryEMI = "select ST.SrNumber, ST.StudentName+' / '+ST.FatherName AS StudentDetails, ST.Address+' , '+ST.Address1 AS Address, ST.MobileNo+' , '+ST.SecMobileNo AS Contect,  FORMAT(FD.FeesDate,'dd/MM/yyyy'), FD.Amount  from FeesDetails FD inner join Student ST on FD.SrNumber=ST.SrNumber where FD.PaymentStatus =@DUE and FORMAT(FD.FeesDate,'dd/MM/yyyy')<=@date";


What I have tried:

C#
private void LoadStudentData()
        {
            try
            {
                string QueryEMI = "select ST.SrNumber, ST.StudentName+' / '+ST.FatherName AS StudentDetails, ST.Address+' , '+ST.Address1 AS Address, ST.MobileNo+' , '+ST.SecMobileNo AS Contect,  FORMAT(FD.FeesDate,'dd/MM/yyyy'), FD.Amount  from FeesDetails FD inner join Student ST on FD.SrNumber=ST.SrNumber where FD.PaymentStatus =@DUE and FORMAT(FD.FeesDate,'dd/MM/yyyy')<=@date";
                var date = dtpDueEMI.Value.ToString("dd/MM/yyyy");
                {
                    con.Open();
                    using (SqlDataAdapter da = new SqlDataAdapter(QueryEMI, con))
                    {
                        da.SelectCommand.Parameters.AddWithValue("@DUE", "DUE");
                        da.SelectCommand.Parameters.AddWithValue("@date", date);
                        DataTable dt = new DataTable();
                        da.Fill(dt);
                        dataGridView1.DataSource = dt;


                        // Change Selected Row Color Just Remove "//"
                        //dataGridView1.DefaultCellStyle.SelectionBackColor = Color.LightBlue;
                        // dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black;

                        // Change DataGridView Rows Color Format Just Remove "//"
                        //dataGridView1.RowsDefaultCellStyle.BackColor = Color.LightGray;
                        //dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightSlateGray;

                        
                    }
                }
            }
            catch
            {
                MessageBox.Show("Fees Not Found");
                this.Close();
            }
        }
Posted
Updated 22-Aug-20 1:07am
v2

1 solution

First off, don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Secondly, never format dates to strings and try to compare them: if you do, you get a string comparison which means that the total result is based on the first different character in the pair of strings. That very bad for sorting or comparing dates as 01-12-2020 is before 31-01-1990.

You use a DateTimePicker to let the user select the date, so don't convert that value to a string, use the value directly and pass that as a DateTime to your query. Compare that DATETIME value with your database DATETIME column FeesDate and the comparison will work.
And probably, so will your code - but fix your whole app to remove the string concatenation, or you will lose your DB.
 
Share this answer
 
Comments
Amar chand123 22-Aug-20 8:16am    
Thank you for answering and hardwork but this time it's all my fault because when i run code in c# winform i forgot put LoadStudentData(); in Form Load that's why query run in SQL but not in winform, i find my mistake when i put breakpoint in my code

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