郁闷的问题:HttpFileCollection 上传文件和DataGrid 问题!!!!在线等!!!!!!!!!
后台代码:
点后进入如下代码:
HttpFileCollection files = HttpContext.Current.Request.Files;
stringstrSQL,strSelect,strTable,strOR,strOR1;
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
try
{
HttpPostedFile postedFile = files[0];
string fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName == " ")
{
Msg.Text = "Please choose a file. ";
return;
}
if (fileName.ToLower().IndexOf( ".txt ") == -1)
{
Msg.Text = "Please choose text file. ";
return;
}
System.IO.Stream UpLoadStream = postedFile.InputStream;
System.IO.StreamReader sr = new System.IO.StreamReader(UpLoadStream, System.Text.Encoding.Default);
int Flg=0;
strOR= " ";
while (sr.Peek() > = 0)
{
Flg=1;
if(Flg==0)
{
strOR= " where ITEM_ID= ' "+sr.ReadLine()+ " ' ";
}
else
{
strOR += " OR ITEM_ID= ' "+sr.ReadLine()+ " ' ";
}
}
strSelect= "select a.ITEM_ID,d.CLNT_NAME_J,c.JOB_CD,c.SERVICE_LINE_CD,c.OFF_CD,c.DEP_CD,(c.FILE_TYPE+ ' '+g.FINAL_CLASS_ST_NAME) as file_type,c.ACCOUNT_MONTH,b.FILE_NO,b.REPORT_CONTENT,(a.STATUS_CD+ ' '+e.FILE_STATUS_NAME) as file_status,(a.ROOM_CD+ ' '+f.ROOM_NAME) as file_location ";
strTable= " FROM ((((((REPORT_FILE_TRN_TBL a left outer join REPORT_FILE_TBL b on (a.ITEM_ID=b.ITEM_ID)) left outer join REPORT_TBL c on (b.MASTER_CD=c.MASTER_CD)) left outer join CLIENT_TBL d on (c.CLIENT_CD=d.CLNT_NBR)) left outer join FILE_STATUS_TBL e on (a.STATUS_CD=e.FILE_STATUS_CD)) left outer join FILE_ROOM_TBL f on (a.ROOM_CD=f.ROOM_CD)) left outer join FINAL_CLASS_TBL g on (c.FIN_MID_CLASS=g.FINAL_CLASS_CD)) ";
strSQL=strSelect+strTable+strOR;
SqlConnection con=cfsnet.createConnection();
con.Open();
SqlDataAdapter sda=new SqlDataAdapter(strSQL,con);
DataSet ds=new DataSet();
sda.Fill(ds, "ITEM_ID ");
this.DataGrid1.DataSource=ds;
this.DataGrid1.DataKeyField= "ITEM_ID ";
this.DataGrid1.DataBind();
con.Close();
sr.Close();
UpLoadStream.Close();
UpLoadStream = null;
sr = null;
strMsg.Append( "***********Succeed!*********** " + " <br> ");
}
catch(System.Exception Ex)
{
strMsg.Append(Ex.Message + " <br> ");
}
Msg.Text = strMsg.ToString();
刚刚进入try 后刚刚执行第一句话就跳到了catch 语句里了,(我用的断点跟踪)随后抱错:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
这是怎么回事啊,各位大狭多多帮忙哦,谢谢!!!!!!!!!
[解决办法]
HttpPostedFile files=System.Web.HttpContext.Current.Request.Files[0];
[解决办法]
file控件不存在