Question Attempted to read or write protected memory.

sanam

New member
Joined
Mar 31, 2014
Messages
1
Programming Experience
3-5
HelloI 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();


}
 
Last edited:
I have removed your code because it was unreadable as it was. Please add a new post and paste your code as plain text inside formatting tags, i.e.

[xcode=c#]your code here[/xcode]
 
Back
Top Bottom