【发布时间】:2010-10-19 22:21:11
【问题描述】:
public DataTable ExcelToDatatable_dt1
{
get
{
foreach (GridViewRow rowItem in GridView1.Rows)
{
CheckBox chk;
// gets the Debit Reference number for each row checked
string productId = GridView1.DataKeys[rowItem.RowIndex].Value.ToString();
chk = (CheckBox)(rowItem.Cells[0].FindControl("RowChecker"));
if (chk.Checked)
{
string fileName = Session["fileName"].ToString();
//string fileName = "Collection.csv";
// put the file name here
string strSql = "SELECT * FROM [" + fileName + "] WHERE DDIREF = ["+ productId +]";
string strCSVConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(".\\Files\\") + ";" + "Extended Properties='text;HDR=YES;'";
using (FileStream filestr = new FileStream(Server.MapPath(".\\Files\\") + "\\schema.ini",
FileMode.Create, FileAccess.Write))
{
using (StreamWriter writer = new StreamWriter(filestr))
{
writer.WriteLine("[" + fileName + "]");
writer.WriteLine("ColNameHeader=False");
writer.WriteLine("Format=CSVDelimited");
writer.WriteLine("DateTimeFormat=dd-MMM-yy");
//Message_ltr.Text += positionList[0].ToString();
for (int i = 0; i < 29; i++)
//if (((positionList[i].ToString() != null) && (positionList[i].ToString() != "")) && ((nameList[i].ToString() != null) && (nameList[i].ToString() != "")))
writer.WriteLine("Col" + positionList[i].ToString() + "=" + nameList[i].ToString() + " Text Width 100");
//writer.WriteLine("Col2=SortCode Text Width 30");
writer.Close();
writer.Dispose();
}
filestr.Close();
filestr.Dispose();
}
OleDbDataAdapter oleda = new OleDbDataAdapter(strSql, strCSVConnString);
DataTable dtbCSV = new DataTable();
oleda.Fill(dtbCSV);
return dtbCSV;
}
}
return null;// instead of null I want to return dtbCSV here
}
这会报错
“错误 45 'DirectDebit.Interbacs.CompanyUpload.ExcelToDatatable_dt1.get':并非所有代码路径都返回值”。
错误是由于返回语句不在正确的范围内。但问题是,我想返回 dtbCSV 并且我无法在“get”范围内返回它,因为 dtCSV 在 get 上下文中不存在,我不知道在返回方法中的范围之间传递值。
【问题讨论】:
标签: c# excel csv checkbox datagridview