Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi friend!
Myself vishal

Please solve my problem and right a correct code. Please it's my request. Problem is that I have to sort a csv file in datagridveiw by use a sql query like this
C#
public static DataSet Getdataset1(string filename1)
       {
           string Connectionstring = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Text;",
         Path.GetDirectoryName(filename1));
           string cmdstring = string.Format(@select * FROM {0} order by IN_NO desc,FIRST_DATE asc,LAST_DATE asc, Path.GetFileName(filename1));
           DataSet dataset = new DataSet();

           using (OleDbConnection olconn = new OleDbConnection(Connectionstring))
           {
               olconn.Open();
               OleDbDataAdapter adapter = new OleDbDataAdapter();
               adapter.SelectCommand = new OleDbCommand(cmdstring, olconn);
               dataset.Clear();
               adapter.Fill(dataset, "Test");
               olconn.Close();

           }

           return dataset;
       }
       public void Process_Click(object sender, EventArgs e)
       {
           try
           {
               DataSet dataset = Form1.Getdataset1(textbox1.Text);
               Datagidveiw1.DataSource = dataset.Tables[0].DefaultView;
           }
           catch (System.Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }
   }//code is:

string cmdstring = string.Format(@select * FROM {0} order by IN_NO desc,FIRST_DATE asc,LAST_DATE asc, Path.GetFileName(filename1));

It works very good but a problem is If IN_NO as a header text like this "IN no" Or "Pn no" sql query not match and operation failed.

I know I can take index number for purticular location but Header text can be change their location as another words I want to say that if In_no In fist csv file at [0] index location but in other file it is on a [2] index so Its a better to take sql query but I think I can also use a case condition and run multiple sql query on this for math but how use In this code , I do not know .

plesae solve my problem.
I will be thankful to you.

:rose:
Posted
Updated 21-Nov-10 3:21am
v2
Comments
Toli Cuturicu 21-Nov-10 9:07am    
Don't lock questions for editiong again.

1 solution

Why don't you try reading the column names first, then if you find "IN no" or "IN_NO" you'll be able to get the correct column index and create a dynamic SQL based on the index you get.

In this way you can also handle if "IN no" or "IN_NO" column name is not found at all.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900