0

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.

Edited by John A: added code tags

2
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by sknake
0

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.

Edited by happygeek: fixed formatting

0

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.

Edited by John A: fixed code tags

0

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]

Edited by sknake: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.