views:

11

answers:

1

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 error 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. Any help or an alternative way will be highly appreciated

A: 

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.

Drackir
hi thnx for sorting out this issue .. really appreciate that. I am having one more problem with the query , In the above example file name is the .csv file . The sql query works fine with the "Select * from ["+ file name +" but it throws an error when I use WHERE DDRIEF= product id;
Error:No value given for one or more required parameters
You're missing a double quote after the second +: `["+ productId +]";` Change it to: `["+ productId + "]";` and see if that helps
Drackir
Actually, after looking at it more, you should remove the square brackets around product ID. The square brackets will make it reference it like a column (unless that's what you want). You should really be using parameters for this. See here: http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbdataadapter.aspx
Drackir