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
Vikram AgrawalPosted May 5, 2015, 9:06 AM
Resign DesignsPosted May 5, 2015, 7:42 AM
Abrar Ahmad AnsariPosted May 5, 2015, 6:54 AM
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();
}
Sanjeeb LenkaPosted May 5, 2015, 6:49 AM
Resign DesignsPosted May 5, 2015, 6:44 AM
by the way it is actually about rows not columns
Abrar Ahmad AnsariPosted May 5, 2015, 6:31 AM
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.