Hi

i want to get contact details from outlook and display that contact details in my datagrid view using csv file. My grid view display only few columns such as Name, Address, Email and category. In outlook i will add the contact details such as First Name, LastName, Business address or Home address and category save these details after that create csv file for added contact folder. My code is here

private

 

DataTable ReadCSV(string Filename) 

{

 


string MyPath = System.IO.Path.GetDirectoryName(Filename);
 

string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + MyPath + ";Extended Properties='text;HDR=Yes;FMT=Delimited'"; 

 


string CmdText = "select [First Name] & '' & [Middle Name] & ' ' & [Last Name] as [Name], [Business Street] & [Business City] as [Address], [E-mail Address] as [Email Id],Categories as [Category] from " + System.IO.Path.GetFileName(Filename);

 

OleDbConnection Con = new OleDbConnection(ConnectionString); 

 


OleDbDataAdapter adptr = new OleDbDataAdapter(CmdText, Con); 

 


DataSet Ds = new DataSet(); 

adptr.Fill(Ds);

 


return Ds.Tables[0]; 

}

after select the csv file i will click 'Show contact' button, at that time i will get Error message as
"Syntax error in From clause". (These error displayed occurred at some times only) i dont know how to solve these. please any one can help me. i need solution urgently.

Welcome to daniweb! Please use code tags when posting code on daniweb:

This line of code is your problem:

string CmdText = "select [First Name] & '' & [Middle Name] & ' ' & [Last Name] as [Name], [Business Street] & [Business City] as [Address], [E-mail Address] as [Email Id],Categories as [Category] from " + System.IO.Path.GetFileName(Filename);

Lets say I have a file named "C:\csv.txt" and I want to select from it. My connection string would be:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\;Extended Properties='text;HDR=Yes;FMT=Delimited'

Notice how I have the C:\ defined there

And the file name is "csv.txt" -- lets do a select from it:

Select * From csv#txt

Notice the file name -- the "." is replaced with "#", and the file extension is included.

Hi, thanks for your reply

I will try above method after that i got same issue.

My changed code is

Private void ButtonClick()
{
                    dataGridContacts.DataSource = ReadCSV(txtCSVFileName.Text);
}

 private DataTable ReadCSV(string Filename)
// FileName ="D:\\Redist\\Contact-Rajeev.CSV"
        {
            string MyPath = System.IO.Path.GetDirectoryName(Filename);
            [B]string file = Filename.Replace('.', '#');[/B]            string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + MyPath + ";Extended Properties='text;HDR=Yes;FMT=Delimited'";
                       string CmdText = "select ([First Name] & '' & [Middle Name] & '' & [Last Name]) as [Name],([Home Street] & '' & [Home City]) as [Address1], ([Business Street]& ' '&[Business City])  as [Address], [E-mail Address] as [EmailId],Categories as [Category] from " + System.IO.Path.GetFileName(file);

                     OleDbConnection Con = new OleDbConnection(ConnectionString);
            OleDbDataAdapter adptr = new OleDbDataAdapter(CmdText, Con);
            DataSet Ds = new DataSet();
            adptr.Fill(Ds);
            return Ds.Tables[0];
        }

Now also am getting error message as "Syntax error in From Clause" contacts are not displayed in data grid view. pls reply me. Advance thanks.

Upload your project please. You're not using code tags and you're not posting the evaluated values of these massively assembled strings.

Please use code tags:

[code=csharp] ...code here... ...notice there are no spaces or capitalizations in the code tag...

[/code]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.