4
   DataRow dr = GetData("select * from Personal_det where Fid='" + va+"'").Rows[0];
    Document doc = new Document(PageSize.A4, 88f, 88f, 10f, 10f);
    Font NormalFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, BaseColor.BLACK);
    using (System.IO.MemoryStream m = new System.IO.MemoryStream())
    {
        PdfWriter w = PdfWriter.GetInstance(doc, m);
        Phrase phrase = null;
        PdfPCell cell = null;
        PdfPTable table = null;
        BaseColor color = null;
        Paragraph para = null;
        Font times = null;
        BaseFont bfTimes = null;


        doc.Open();
        table = new PdfPTable(2);

        cell = PhraseCell(new Phrase("Faculty Profile", FontFactory.GetFont("Arial", 12, Font.UNDERLINE, BaseColor.BLACK)), PdfPCell.ALIGN_CENTER);
        //table.SpacingBefore = 20f;

        cell.Colspan = 2;
        table.AddCell(cell);
        cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
        cell.Colspan = 2;
        cell.PaddingBottom = 30f;

错误信息 这是我的 C# 代码。当我尝试执行时,它会出现以下错误

我不知道我从哪里得到错误,是代码本身还是数据库。在某些情况下,此代码有效,但在某些情况下,它会给出以下错误。你能向我澄清错误吗?

如果我使用 dr.rows.length ..它不显示行关键字未显示行关键字

    private DataTable GetData(string query)
      {
    string conString =                              
     ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection cn = new SqlConnection(conString))
    {
        using (SqlDataAdapter da = new SqlDataAdapter())
        {
            cmd.Connection = cn;
            da.SelectCommand = cmd;
            using (DataTable dt = new DataTable())
            {
                da.Fill(dt);
                return dt;
            }


        }
    }


}

这是 Getdata 方法..以前我将 FID 作为字符串但现在转换为 varchar,我正在努力获得正确的答案,请帮助重建我的代码

4

5 回答 5

2

希望这有效....首先检查 dt 的内容,然后选择它的行

 var dt = GetData("select * from Personal_det where Fid='" + Session["FID"] + "'");
        if (dt != null && dt.Rows.Count > 0)
        {
            DataRow dr = dt.Rows[0];// or do somthing

        }
          else
         {
           //No data

         }
于 2013-09-17T09:42:00.220 回答
1

看起来您的查询没有返回任何行...因此出现异常。

尝试这样的事情......

var dt = GetData(".....your query");
if(dt != null && dt.Rows.Length > 0)
{
    var dr= dt.Rows[0]
    //do your stuff..
}
于 2013-09-16T04:42:22.493 回答
1

执行以下操作

DataTable dt = GetData("select * from Personal_det where Fid='" + va+"'");

如下进行一些验证

if(dt != null && dt.Rows.Count > 0)
{

  DataRow dr = dt.Rows[0];
  // do something with dr.........

}
于 2013-09-16T05:05:31.220 回答
0

查询未返回任何行。您应该在尝试访问数据之前检查长度。

var dataTable = GetData(....);
if(dataTable.Rows.Length > 0) 
{
  // you can read row 0
}
else 
{ 
  // no rows
}
于 2013-09-16T04:44:21.803 回答
0

如果这种情况偶尔发生(有时有行,有时没有),那么它是数据问题。您应该在访问该行之前进行验证,以便处理从此查询返回的零记录

         DataRow dr = GetData("select * from Personal_det where Fid='" + va+"'");

         if (dr.Rows.Length> 0 )
          {
          //post your logic
          }
于 2013-09-16T04:45:29.503 回答