Click here to Skip to main content
15,901,505 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
string page = Request.Url.Segments[Request.Url.Segments.Length - 1];
       DataTable dtMeta = this.GetData(page);

       //Add Page Title
       this.Page.Title = dtMeta.Rows[0]["Title"].ToString();

       //Add Keywords Meta Tag
       HtmlMeta keywords = new HtmlMeta();
       keywords.HttpEquiv = "keywords";
       keywords.Name = "keywords";
       keywords.Content = dtMeta.Rows[0]["Keywords"].ToString();
       this.Page.Header.Controls.Add(keywords);

       //Add Description Meta Tag
       HtmlMeta description = new HtmlMeta();
       description.HttpEquiv = "description";
       description.Name = "description";
       description.Content = dtMeta.Rows[0]["Description"].ToString();
       this.Page.Header.Controls.Add(description);

C#
private DataTable GetData(string page)
   {
       string query = "SELECT Title, Description, Keywords FROM Titles__Metatags WHERE LOWER(Page) = LOWER(@Page)";
       string constr = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
       using (SqlConnection con = new SqlConnection(constr))
       {
           using (SqlCommand cmd = new SqlCommand(query))
           {
               using (SqlDataAdapter sda = new SqlDataAdapter())
               {
                   cmd.CommandType = CommandType.Text;
                   cmd.Parameters.AddWithValue("@Page", page);
                   cmd.Connection = con;
                   sda.SelectCommand = cmd;
                   DataTable dt = new DataTable();
                   sda.Fill(dt);
                   return dt;
               }
           }
       }
   }

plz help me....this error
Posted
Updated 5-Apr-15 21:04pm
v2
Comments
King Fisher 6-Apr-15 3:05am    
In which line do you get the error?
King Fisher 6-Apr-15 3:06am    
There is no row in your DataTable.
aarif moh shaikh 6-Apr-15 3:18am    
Check you query .. may be it's returning 0 records.....

Make sure that in the following line, you have data in the datatable
C#
DataTable dtMeta = this.GetData(page);

This exception occurs when you don't have rows in the datatable but you are trying to get data from a column or row.
I believe you may be getting exception in the following line
C#
description.Content = dtMeta.Rows[0]["Description"].ToString();


How to resolve:
Check the query you are using to retrive data. Check the value in the parameter.
SQL
SELECT Title, Description, Keywords FROM Titles__Metatags WHERE LOWER(Page) = LOWER(@Page)";


Hope, it helps :)
 
Share this answer
 
v2
The error says, there is no row in your dataTable to read , better you check Whether the DataTable have values or not before you read it.
 
Share this answer
 
v2
check the row count before you read row data, for example
C#
if(dtMeta.Rows.Count  > 0 ){
      this.Page.Title = dtMeta.Rows[0]["Title"].ToString();
      // your other code....
}
 
Share this answer
 

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