Hi,
I have this code at page load to and it loads some data from db to form.
try
{
connection.Open();
adapter.SelectCommand = new SqlCommand("select * from mydb order by LangStaticID ASC", connection);
adapter.Fill(ds);
connection.Close();
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
PageTitle.Text = ds.Tables[0].Rows[0].ItemArray[2].ToString();
MetaTitle.Text = ds.Tables[0].Rows[0].ItemArray[3].ToString();
{
connection.Open();
adapter.SelectCommand = new SqlCommand("select * from mydb order by LangStaticID ASC", connection);
adapter.Fill(ds);
connection.Close();
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
PageTitle.Text = ds.Tables[0].Rows[0].ItemArray[2].ToString();
MetaTitle.Text = ds.Tables[0].Rows[0].ItemArray[3].ToString();
}
when there are no results it gives me this error

when there are no results it gives me this error
How can I handle this error?
tried with this but no luck
if (ds.Tables[0].Rows.Count == 0)
{
//
}
tried with this but no luck
if (ds.Tables[0].Rows.Count == 0)
{
//
}
Thanks

Abrar Ahmad AnsariRank #3,390 · 425 reputationPosted May 5, 2015, 2:31 AM EDT
how many columns do you have in ur table?
there is no 4 column in ur table that's the reason it caught the error.
Resign Designs35 reputationPosted May 5, 2015, 2:44 AM EDT
by the way it is actually about rows not columns
Sanjeeb LenkaRank #301 · 8.3K reputationPosted May 5, 2015, 2:49 AM EDT
Abrar Ahmad AnsariRank #3,390 · 425 reputationPosted May 5, 2015, 2:54 AM EDT
use below code. it will handle the error.
if(ds.Tables.Count>0 && ds.Tables[0].Rows.Count>0)
{
PageTitle.Text = ds.Tables[0].Rows[0][2].ToString();
MetaTitle.Text = ds.Tables[0].Rows[0][3].ToString();
}
Resign Designs35 reputationPosted May 5, 2015, 3:42 AM EDT
Vikram AgrawalRank #4,713 · 329 reputationPosted May 5, 2015, 5:06 AM EDT