0

I'm trying to compare a date obtained from maskedTextBox accepting short date, with a date from an excel sheet.
I need to select those rows from excel file that are equal with the date from the maskedTextBox and display them in datagridview.
The problem is that i don't get any results and compiling the code gives no errors.
My code :

      private void button13_Click(object sender, EventArgs e)
        {

            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\alekan\Desktop\alm2.xlsx" + @";Extended Properties=""Excel 12.0 Xml;HDR=YES;ImportMixedTypes=Text;TypeGuessRows=0""";
            OleDbCommand command = new OleDbCommand("SELECT txt" + " " + "FROM [sheet2$] WHERE HHMM = '"+ maskedTextBox1.Text+"'", conn);

            DataSet dstxt = new DataSet();
            OleDbDataAdapter adapter = new OleDbDataAdapter(command);
            adapter.Fill(dstxt);
            dataGridView1.DataSource = dstxt.Tables[0];
        }

p.s. txt and HHMM is named columns
any help is GREATLY apreciated!!!!!

2
Contributors
1
Reply
17
Views
2 Years
Discussion Span
Last Post by __avd
0

Use @parameterized sql statement and that want you can compare data without formating data into string.

Example:

string sql = "select txt from [Sheet2$] where HHMM=@dateParam";
OleDbCommand command = new OleDbCommand(sql,conn);
command.Parameters.AddWithValue("@dateParam",maskedTextBox1.Text);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.