Click here to Skip to main content
13,351,044 members (61,534 online)

.NET Framework

 
GeneralRe: Process data as you type Pin
Eddy Vluggen14-Oct-17 3:40
professionalEddy Vluggen14-Oct-17 3:40 
GeneralRe: Process data as you type Pin
Dirk Bahle14-Oct-17 8:27
memberDirk Bahle14-Oct-17 8:27 
GeneralRe: Process data as you type Pin
Eddy Vluggen17-Oct-17 3:06
professionalEddy Vluggen17-Oct-17 3:06 
AnswerRe: Process data as you type Pin
Gerry Schmitz14-Oct-17 9:35
memberGerry Schmitz14-Oct-17 9:35 
GeneralRe: Process data as you type Pin
Dirk Bahle16-Oct-17 4:15
memberDirk Bahle16-Oct-17 4:15 
AnswerRe: Process data as you type Pin
Pete O'Hanlon14-Oct-17 12:47
protectorPete O'Hanlon14-Oct-17 12:47 
GeneralRe: Process data as you type Pin
Dirk Bahle16-Oct-17 4:19
memberDirk Bahle16-Oct-17 4:19 
QuestionAn unhandled exception of type 'System.AccessViolationException' occurred in System.Data.dll Pin
indian1434-Oct-17 16:30
memberindian1434-Oct-17 16:30 
Hi,

I am getting the error :
An unhandled exception of type 'System.AccessViolationException' occurred in System.Data.dll

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. 
when I am trying to read excel file in my Console Application, I am also researching and googling but not finding the appropriate answer that can fix. The error is happening at conn.Open() call, is it related to opening file, then is there any way I can just open the file in read mode and read the values from it because all I need is to read the file and output/write those values into PipeDelimited CSV file.

Here is the code that I have:
public static string ConvertXlsAndXlstToPipeDelimitedFile(string strSourceFilePath, bool isFirstRowColumnHeader, string sheetName)
{
    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
        strSourceFilePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";

    OleDbConnection conn = null;
    StreamWriter wrtr = null;
    OleDbCommand cmd = null;
    OleDbDataAdapter da = null;

    string destinationFilePath = string.Empty;

    conn = new OleDbConnection(strConn);

    try
    {
        if (conn.State != ConnectionState.Open)
        {
            conn.Open();
        }

        cmd = new OleDbCommand("SELECT * FROM [" + sheetName + "$]", conn); //"Sheet1"
        cmd.CommandType = CommandType.Text;

        da = new OleDbDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);

        string strDirectory = (System.IO.Path.GetDirectoryName(strSourceFilePath));

        if (!System.IO.Directory.Exists(strDirectory))
        {
            System.IO.Directory.CreateDirectory(strDirectory);
        }

        Path.GetFileNameWithoutExtension(strSourceFilePath);

        destinationFilePath = (strDirectory + (@"\" + (Path.GetFileNameWithoutExtension(strSourceFilePath) + ".csv")));

        wrtr = new StreamWriter(destinationFilePath);

        int ColCount = 0; string rowString = "";
        for (int y = 0; y < dt.Columns.Count; y++)
        {
            rowString += dt.Columns[y].ColumnName + "|";

        }
        wrtr.WriteLine(rowString);

        for (int x = 0; x < dt.Rows.Count; x++)
        {
            rowString = "";
            for (int y = 0; y < dt.Columns.Count; y++)
            {
                rowString += "\"" + dt.Rows[x][y].ToString() + "\"|";
            }
            wrtr.WriteLine(rowString);
        }

        if (File.Exists(destinationFilePath))
            return destinationFilePath;
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        if (conn != null)
        {
            if (conn.State != ConnectionState.Closed)
            {
                conn.Close();
            }
            conn.Dispose();
        }

        if (cmd != null)
        {
            cmd.Dispose();
        }
        if (da != null)
        {
            da.Dispose();
        }

        if (wrtr != null)
        {
            wrtr.Close();
            wrtr.Dispose();
        }
    }
    //MessageBox.Show("Done");
    return string.Empty;
}

Can anybody please help me why am I getting that error and is there any fix for it? - Thanks in advance.

Thanks,

Abdul Aleem

"There is already enough hatred in the world lets spread love, compassion and affection."
AnswerRe: An unhandled exception of type 'System.AccessViolationException' occurred in System.Data.dll Pin
Richard MacCutchan4-Oct-17 23:22
protectorRichard MacCutchan4-Oct-17 23:22 
GeneralRe: An unhandled exception of type 'System.AccessViolationException' occurred in System.Data.dll Pin
indian1435-Oct-17 4:08
memberindian1435-Oct-17 4:08 
GeneralRe: An unhandled exception of type 'System.AccessViolationException' occurred in System.Data.dll Pin
Richard MacCutchan5-Oct-17 4:14
protectorRichard MacCutchan5-Oct-17 4:14 
GeneralRe: An unhandled exception of type 'System.AccessViolationException' occurred in System.Data.dll Pin
indian1435-Oct-17 4:18
memberindian1435-Oct-17 4:18 
GeneralRe: An unhandled exception of type 'System.AccessViolationException' occurred in System.Data.dll Pin
Richard MacCutchan5-Oct-17 4:27
protectorRichard MacCutchan5-Oct-17 4:27 
GeneralRe: An unhandled exception of type 'System.AccessViolationException' occurred in System.Data.dll Pin
indian1435-Oct-17 4:34
memberindian1435-Oct-17 4:34 
GeneralRe: An unhandled exception of type 'System.AccessViolationException' occurred in System.Data.dll Pin
indian1435-Oct-17 8:46
memberindian1435-Oct-17 8:46 
GeneralRe: An unhandled exception of type 'System.AccessViolationException' occurred in System.Data.dll Pin
Richard MacCutchan5-Oct-17 22:03
protectorRichard MacCutchan5-Oct-17 22:03 
AnswerRe: An unhandled exception of type 'System.AccessViolationException' occurred in System.Data.dll Pin
Eddy Vluggen5-Oct-17 4:45
professionalEddy Vluggen5-Oct-17 4:45 
GeneralRe: An unhandled exception of type 'System.AccessViolationException' occurred in System.Data.dll Pin
indian1435-Oct-17 8:46
memberindian1435-Oct-17 8:46 
GeneralRe: An unhandled exception of type 'System.AccessViolationException' occurred in System.Data.dll Pin
Eddy Vluggen6-Oct-17 2:51
professionalEddy Vluggen6-Oct-17 2:51 
QuestionImport Date values from Pipe-Delimited file to SQL Server table Pin
indian1433-Oct-17 22:07
memberindian1433-Oct-17 22:07 
SuggestionRe: Import Date values from Pipe-Delimited file to SQL Server table Pin
Richard MacCutchan3-Oct-17 22:38
protectorRichard MacCutchan3-Oct-17 22:38 
AnswerRe: Import Date values from Pipe-Delimited file to SQL Server table Pin
Mycroft Holmes3-Oct-17 22:38
professionalMycroft Holmes3-Oct-17 22:38 
GeneralRe: Import Date values from Pipe-Delimited file to SQL Server table Pin
indian1434-Oct-17 7:58
memberindian1434-Oct-17 7:58 
GeneralRe: Import Date values from Pipe-Delimited file to SQL Server table Pin
Richard MacCutchan4-Oct-17 23:24
protectorRichard MacCutchan4-Oct-17 23:24 
GeneralRe: Import Date values from Pipe-Delimited file to SQL Server table Pin
indian1435-Oct-17 4:15
memberindian1435-Oct-17 4:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy |
Web03 | 2.8.180111.1 | Last Updated 19 Jan 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid