No line at position 0: C # code error

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; 

Error messsage This is my C # code. When I try to execute it, you get the following error:

I don’t know where I get the error from: this is from the code itself or the database. In some cases, this code works, but for some instance it will give the following error. Can you clarify my mistake.?

If I use dr.rows.length..It does not show the row keyword No row keyword shown

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

This is the Getdata method. Previously, I had FID as a string, but now converted to varchar, I'm struggling to get the correct answer, please help restore my code

+4
source share
5 answers

hope this works ... first check the dt for the content, then select its line

  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 } 
+2
source

It seems that there was no line returned by your request ... hence the exception.

try something like this ...

 var dt = GetData(".....your query"); if(dt != null && dt.Rows.Length > 0) { var dr= dt.Rows[0] //do your stuff.. } 
+1
source

Do as below

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

do some check as below

 if(dt != null && dt.Rows.Count > 0) { DataRow dr = dt.Rows[0]; // do something with dr......... } 
+1
source

There were no rows in the request. You must check the length before trying to access the data.

 var dataTable = GetData(....); if(dataTable.Rows.Length > 0) { // you can read row 0 } else { // no rows } 
0
source

If this happens sometimes (sometimes there are lines, and sometimes not), then this is a data problem. you must check before accessing the string to handle the null records returned from this query.

  DataRow dr = GetData("select * from Personal_det where Fid='" + va+"'"); if (dr.Rows.Length> 0 ) { //post your logic } 
0
source

Source: https://habr.com/ru/post/1502336/


All Articles