I have simple code, like this: http://paste.php.lv/9aad9c370b9104e5e42908c8db0f1356?lang=csharp
I have problem if I enter SELECT id2 FROM table and column id2 doesn't not exist it's shows me error:
http://uploadpic.org/storage/2011/KyrNTJB1zK8blk8WV17VwbLlt.png
So I tried many ways but nothing helps:
1) if (ds != null)
2) if ((ds != null) && (ds.Tables[0].Rows.Count != 0))
I searched it all around google so no help there.
Help me please.
Loading
Zoran HorvatPosted Jul 8, 2011, 4:56 AM
You are accessing ds before you have filled it (only new DataSet() has been executed so that ds does not equal null at the moment).
You should do dscmd.Fill(ds); before line if (ds != null). However, dscmd.Fill(ds) must be enclosed into a try...catch block so that you can handle exceptions caused by bad query for example. So basically your code might look like this:
using (...)
{
Acces2.Open();
OleDbDataAdapter dscmd = new OleDbDataAdapter("SELECT id2, atvk, atvk_nos, adrese, uzvards, vards, dzim_date, reize, nedela, [no], lidz, telefons AS telef FROM " + value3 + "man", Acces2);
DataSet ds = new DataSet();
try
{
dscmd.Fill(ds);
}
catch (System.Exception ex)
{
// report ex in any way applicable
ds = null; // this indicates that there was an error filling the dataset
}
if (ds != null)
{
dataGridView1.DataSource = ds.Tables[0];
...
}
...
}
Janis PeksaPosted Jul 8, 2011, 5:00 AM
Thank you.
Working good.