Click here to Skip to main content
15,905,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I try Show Data in datagridview in C# Form AccessDatabase data show Correctly
but when i try filter data by LoanNumber This Error Show


An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: Data type mismatch in criteria expression.


If i Delete this Statement from where "LoanNumber="+textBoxLoanNumber.Text+"
then Code Work Fine but in need show only Filter Data

What I have tried:

C#
private void loanDataLoad()
        {
            String connstring = ConfigurationManager.ConnectionStrings["FincorpData"].ConnectionString;
            OleDbConnection cnn = new OleDbConnection(connstring);
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            string sql = "select InstallmentNumber, LoanNumber, InstallmentDate, LoanAmount, PaymentStatus from installment where LoanNumber="+textBoxLoanNumber.Text+"";
            DataSet ds = new DataSet();
            {
                   cnn.Open();
                   adapter = new OleDbDataAdapter(sql,cnn);
                   adapter.Fill(ds);
                   cnn.Close();
                   dataGridView1.DataSource = ds.Tables[0];
            }
        }
Posted
Updated 18-Mar-20 20:54pm
v2

1 solution

Do not do it like that - and ignore KarstenK as well!
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?
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlDataAdapter da = new SqlDataAdapter("SELECT MyColumn1, MyColumn2 FROM myTable WHERE mySearchColumn = @SEARCH", con))
        {
        da.SelectCommand.Parameters.AddWithValue("@SEARCH", myTextBox.Text);
        DataTable dt = new DataTable();
        da.Fill(dt);
        myDataGridView.DataSource = dt;
        }
    }
 
Share this answer
 
Comments
KarstenK 19-Mar-20 4:28am    
you are right, I remover my answer.
Amar chand123 19-Mar-20 4:47am    
Thank you both

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