Home > Net >  C # the Dataset error for help
C # the Dataset error for help

Time:03-16

Error:
System. IndexOutOfRangeException
HResult=0 x80131508
Message=at position 0 no line,
Source=System. Data
StackTrace:
The ats System. Data. RBTree ` 1. GetNodeByIndex (Int32 userIndex)
Ats System. Data. The DataRowCollection. Get_Item (Int32 index)
The at WindowsFormsApp1. Form1. Button1_Click (Object sender, EventArgs e) in C: \ Users \ admin \ source \ repos \ WindowsFormsApp1 \ WindowsFormsApp1 \ Form1 cs: line 91
Ats System. Windows. Forms. Control. The OnClick (EventArgs e)
Ats System. Windows. Forms. The Button. The OnClick (EventArgs e)
Ats System. Windows. Forms. The Button. The onm ouseUp (MouseEventArgs mevent)
Ats System. Windows. Forms. Control. WmMouseUp (Message& M, MouseButtons button, Int32 on)
Ats System. Windows. Forms. Control. WndProc (Message& M)
Ats System. Windows. Forms. ButtonBase. WndProc (Message& M)
Ats System. Windows. Forms. Button. WndProc (Message& M)
Ats System. Windows. Forms. NativeWindow. DebuggableCallback (IntPtr hWnd, Int32 MSG, IntPtr wparam, IntPtr lparam)
Ats System. Windows. Forms. UnsafeNativeMethods. DispatchMessageW (MSG& MSG)
At System.Windows.Forms.Application.Com ponentManager. System. Windows. Forms. UnsafeNativeMethods. IMsoComponentManager. FPushMessageLoop (IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
Ats System. Windows. Forms. Application. ThreadContext. RunMessageLoopInner (Int32 reason, ApplicationContext context)
Ats System. Windows. Forms. Application. ThreadContext. RunMessageLoop (Int32 reason, ApplicationContext context)
The at WindowsFormsApp1. The Program. The Main () in C: \ Users \ admin \ source \ repos \ WindowsFormsApp1 \ WindowsFormsApp1 \ Program cs: line 19

The code is as follows:
SqlConnection lo_conn=new SqlConnection (" Server=192.168.1.86; The Database=AIS20200828050347; Uid=sa; The PWD=1 qaz @ WSX ");
Lo_conn. The Open ();
SqlCommand lo_cmd=new SqlCommand ();//create the command object
Lo_cmd.Com mandText="SELECT FNUMBER as item number, FSORTNAME as project name, FCREATEDATE as creation date FROM BDI_t_GCXM_DA" +
"Where FCREATEDATE & gt; Dateadd (s, 1, \ '+ LastTime + "
"\ ') and FSORTNAME not like '% repeat %' and FSORTNAME not like '% %' test order by DESC" created date;//write SQL statement
Lo_cmd. Connection=lo_conn;//specify the connection object that created above
SqlDataAdapter dbAdapter=new SqlDataAdapter (lo_cmd);//return the result set in the form of data set
The DataSet ds=new DataSet ();//create the data set object
DbAdapter. The Fill (ds);//use the returned result set populate the data set, the data set can be able to operate data controls DataBind, the other is played a yourself


if (ds) Tables [0]. Rows [0] [0]! Value)=DBNull.
- error position{
String strExcelFileName;
StrExcelFileName=AppPath + "\ \ project list. XLSX";
ExportExcel (ds, strExcelFileName);
MessageBox. Show (" to obtain a list project success!" );

String writecontent=ds. Tables [0]. Rows [0] [2]. The ToString ();//will be a latest data assigned to writecontent


//will be the latest date written text file
FileStream fs=new FileStream (AppPath + "\ \ record. TXT", FileMode. Create);
StreamWriter sw=new StreamWriter (fs);
Sw. Write (writecontent);//write
Sw. Flush ();//to empty buffer
sw.Close();//off flow
fs.Close();
}
The else {
MessageBox. Show (" your project is the latest information!" );
}

CodePudding user response:

Dataset is empty when the above error, add the statement seemed futile

CodePudding user response:

To determine whether the ds is Null to determine ds. Name count> 0

CodePudding user response:

See the code, the ds should not empty, ds. Tables should also exist, suggested that determine the ds. Tables [0]. Rows. If the Count is greater than zero, also can use the
If (ds) Tables [0]. Rows [0] [0]. The ToString (). Length> 0)
{
//do STH
}
This method of judgement
  •  Tags:  
  • C#
  • Related