CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2014
    Posts
    2

    Attempted to read or write protected memory.

    Hello
    I have two from in visual studio 2010 with C#.
    In form1 I have one combobox and one button .In form2 I have Reportviewer . When Button in form1 is clicked form2 is shown. Now my problem is when I click button to show form2 on the line Form2.Show(); this error appear :
    Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
    What can i do?
    In form1 I have one combobox that combo fill with Database with this code in the Form Load:
    private void DocReportOnlyAlalhesabSearch_Load(object sender, EventArgs e)
    {


    //fill shahrestan Combo
    if (Class.Global.CurrentYear.Length != 0)
    {
    OleDbConnection conn1;
    OleDbCommand cmd1;
    cmd1 = new OleDbCommand();
    string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|" + "\\DB\\" + Class.Global.CurrentYear;
    conn1 = new OleDbConnection(connectionstring);
    conn1.Open();
    cmd1.CommandType = CommandType.Text;

    try
    {

    cmd1 = new OleDbCommand("SELECT * From tblShahrestanName ;", conn1);


    OleDbDataReader myreader = cmd1.ExecuteReader();

    int countrow;
    countrow = 0;
    cmbNameShahrestan.Items.Clear();
    if (myreader.HasRows)
    {

    while (myreader.Read())
    {
    cmbNameShahrestan.Items.Insert(countrow, myreader["ShahrestanName"].ToString().Trim());
    countrow = countrow + 1;
    }

    }
    else
    {
    cmbNameShahrestan.Items.Clear();

    }

    myreader.Dispose();
    myreader.Close();




    }
    catch (Exception Ar)
    {
    MessageBox.Show(Ar.Message);
    }

    finally
    {
    cmd1.Dispose();
    conn1.Dispose();
    conn1.Close();
    }
    }

    //end fill shahrestan combo


    }
    In a button click on the form1 I have this code :
    private void btnSearch_Click(object sender, EventArgs e)
    {
    Form2 Form2 = new Form2();

    Form2.Show();




    }

    In form2 I have A report viewer in form load of that I have this code:
    private void Form2_Load(object sender, EventArgs e)
    {
    OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|" + "\\DB\\" + Class.Global.CurrentYear);
    OleDbDataAdapter da = new OleDbDataAdapter("SELECT * from DocReportAlalhesabJoda", con);

    DataSet ds = new DataSet();

    da.Fill(ds);

    ds.Tables[0].TableName = "DataTableDocReportAlalhesabJoda";




    reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", ds.Tables[0]));

    this.reportViewer1.RefreshReport();


    }

  2. #2
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Attempted to read or write protected memory.

    Code:
    Form2 Form2 = new Form2();
    You cannot d this. as yu have the same Name for the class and the variable.

    You Need to write someting like
    Code:
    Form2 frm = new Form2();
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured