位置0没有行。 [英] There is no row at position 0.

查看:62
本文介绍了位置0没有行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  string  page = Request.Url.Segments [Request.Url.Segments.Length  -   1 ]; 
DataTable dtMeta = this .GetData(page);

// 添加页面标题
this .Page.Title = dtMeta.Rows [ 0 ] [ 标题]。ToString();

// 添加关键字元标记
HtmlMeta keywords = < span class =code-keyword> new
HtmlMeta();
keywords.HttpEquiv = keywords;
keywords.Name = keywords;
keywords.Content = dtMeta.Rows [ 0 ] [ 关键字]的ToString();
this .Page.Header.Controls.Add(keywords);

// 添加说明元标记
HtmlMeta description = < span class =code-keyword> new
HtmlMeta();
description.HttpEquiv = description;
description.Name = description;
description.Content = dtMeta.Rows [ 0 ] [ 描述]的ToString();
this .Page.Header.Controls.Add(description);



  private  DataTable GetData( string  page)
{
string query = SELECT标题,描述,关键词从标题_Metatags WHERE LOWER(Page)= LOWER(@Page);
string constr = ConfigurationManager.ConnectionStrings [ CON]的ConnectionString。
使用(SqlConnection con = new SqlConnection(constr))
{
使用(SqlCommand cmd = new SqlCommand(query))
{
使用(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帮我....这个错误

解决方案

确保在以下行中,数据表中有数据

 DataTable dtMeta =  this  .GetData(page); 



如果数据表中没有行,则会发生此异常试图从列或行获取数据。

我相信你可能会在以下行中出现异常

 description.Content = dtMeta .Rows [ 0 ] [ 描述]。ToString(); 





如何解决:

检查您用于检索的查询数据。检查参数中的值。

  SELECT 标题,描述,关键字 FROM 标题_Metatags  WHERE  LOWER(Page)= LOWER( @ Page  ;  





希望,它有帮助:)


错误说,你的dataTable中没有行可读,更好的是你检查 DataTable 在读取之前是否有值。


在读取行数据之前检查行计数,例如

 < span class =code-keyword> if (dtMeta.Rows.Count >   0 ){
this .Page.Title = dtMeta.Rows [ 0 ] [ 标题]的ToString();
// 您的其他代码....
}


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);


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

解决方案

Make sure that in the following line, you have data in the datatable

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

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.

SELECT Title, Description, Keywords FROM Titles__Metatags WHERE LOWER(Page) = LOWER(@Page)";



Hope, it helps :)


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.


check the row count before you read row data, for example

if(dtMeta.Rows.Count  > 0 ){
      this.Page.Title = dtMeta.Rows[0]["Title"].ToString();
      // your other code....
}


这篇关于位置0没有行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆