報錯:
System.IndexOutOfRangeException
HResult=0x80131508
Message=在位置 0 處沒有任何行。
Source=System.Data
StackTrace:
at System.Data.RBTree`1.GetNodeByIndex(Int32 userIndex)
at System.Data.DataRowCollection.get_Item(Int32 index)
at WindowsFormsApp1.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\admin\source\repos\WindowsFormsApp1\WindowsFormsApp1\Form1.cs:line 91
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at WindowsFormsApp1.Program.Main() in C:\Users\admin\source\repos\WindowsFormsApp1\WindowsFormsApp1\Program.cs:line 19
代碼如下:
SqlConnection lo_conn = new SqlConnection("Server=192.168.1.86;Database=AIS20200828050347;uid=sa;pwd=1qaz@wsx");
lo_conn.Open();
SqlCommand lo_cmd = new SqlCommand(); //創建命令物件
lo_cmd.CommandText = "SELECT FNUMBER as 專案編號, FSORTNAME as 專案名稱 ,FCREATEDATE as 創建日期 FROM BDI_t_GCXM_DA " +
" where FCREATEDATE > dateadd(s,1,\'" + LastTime +
"\') and FSORTNAME not like '%重復%' and FSORTNAME not like '%測驗%' order by 創建日期 DESC"; //寫SQL陳述句
lo_cmd.Connection = lo_conn; //指定連接物件,即上面創建的
SqlDataAdapter dbAdapter = new SqlDataAdapter(lo_cmd); //以資料集的方式反回結果集
DataSet ds = new DataSet(); //創建資料集物件
dbAdapter.Fill(ds); //用回傳的結果集填充資料集,這個資料集可以被能操作資料的控制元件DataBind,其它的就自己發揮了吧
if (ds.Tables[0].Rows[0][0] != DBNull.Value) ---報錯位置
{
string strExcelFileName;
strExcelFileName = AppPath + "\\專案串列.xlsx";
ExportExcel(ds, strExcelFileName);
MessageBox.Show("獲取專案串列成功!");
string writecontent = ds.Tables[0].Rows[0][2].ToString(); //將最新一條資料賦值給writecontent
//將最新日期寫入文本檔案
FileStream fs = new FileStream(AppPath + "\\record.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(writecontent); //開始寫入
sw.Flush(); //清慷訓沖區
sw.Close(); //關閉流
fs.Close();
}
else {
MessageBox.Show("你的專案資訊已經是最新的了!");
}
uj5u.com熱心網友回復:
dataset是空的時候就報上述錯誤,添加的判斷陳述句貌似沒啥用uj5u.com熱心網友回復:
先判斷ds是否為Null再判斷ds.tables.count>0uj5u.com熱心網友回復:
看代碼的情況,ds應該不會為空,ds.Tables也應該存在的,建議判斷ds.Tables[0].Rows.Count是否大于0,另外可以使用if(ds.Tables[0].Rows[0][0].ToString().Length>0)
{
//do sth
}
這種方法判斷處理
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/268472.html
標籤:C#
上一篇:怎么做一個上位機軟體,實作RS232串口與我的單片機通信?簡單一點的方法
下一篇:C#掉用C++執行緒
