开发者

Cannot return datatable

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  
        }  

This gives an error

"Error 45 'DirectDebit.Interbacs.CompanyUpload.ExcelToDatatable_dt1.get': not all code paths return a value".

The e开发者_高级运维rror is due to the return statement which is not in the correct scope. But the problem is, I want to return dtbCSV and I am not able return that within the "get" scope as dtCSV does not exist within the get context, I have no knowledge of passing the values between scopes within a return method.


Try putting DataTable dtbCSV = new DataTable(); before your foreach statement (and remove the other declaration).

Edit
Note: This will put dtbCSV in the scope of the entire get block instead of just in your if statement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜