attempted to read write protected memory. this is often an indication that other memory is corrupt [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have developed a desktop application using C# and sql server compact 3.5
I have import all necessary dll's
System.Data.SqlServerCe.dll
sqlceca35.dll
sqlcecompact35.dll
sqlceer35EN.dll
sqlceme35.dll
sqlceoledb35.dll
sqlceqp35.dll
sqlcese35.dll
And deploy it while it is running without any error on this PC when i install setup on client machine it will do insertion accurately in database.sdf and also retrieve data for auto-complete.
When I want to retrieve data from it to fill combo box or grid it will generate error
attempted to read write protected memory.
this is often an indication that other memory is corrupt
Note: if I install this setup on another pc which have VS 2008 on it will work fine without any error. Will I have to install something on the Client Pc?
I also try to build
in VS2008:
Tools->Options
Debugging->General
uncheck option "Suppress JIT optimization on module load"
but result is same.
Here is a class that I used for store and retrive data from db
class dataBase
{
private SqlCeDataAdapter ad;
private SqlCeCommand cmd;
private string StringdbFileName=("Data Source=" + System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\sp.sdf").Replace(#"file:\", "");
private SqlCeConnection coon;
public dataBase()
{
coon = new SqlCeConnection(StringdbFileName);
coon.Close();
}
public int ExecuteSQL(string Query)
{
try
{
coon.Open();
cmd = new SqlCeCommand();
cmd.Connection = this.coon;
cmd.CommandText = Query;
return cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
coon.Close();
}
}
public DataTable GetDataTable(string Query)
{
try
{
coon.Open();
DataTable dt = new DataTable();
cmd = new SqlCeCommand();
cmd.CommandText = Query;
ad = new SqlCeDataAdapter(Query,coon);
ad.Fill(dt);
return dt;
}
catch (Exception ex)
{
throw ex;
}
finally
{
coon.Close();
}
}
public void FillComboBox(string Query, string DisplayMember,string ValueMember, ComboBox cmb)
{
try
{
coon.Open();
DataTable dt = new DataTable();
cmd = new SqlCeCommand();
cmd.CommandText = Query;
ad = new SqlCeDataAdapter(Query, coon);
ad.Fill(dt);
cmb.DataSource = dt;
cmb.DisplayMember = DisplayMember;
cmb.ValueMember = ValueMember;
}
catch (Exception ex)
{
throw ex;
}
finally
{
coon.Close();
}
}
}

From my experience, this happens when you access the same SqlConnection from different threads.
SQL CE is not very thread-friendly.

Related

index was out of range. must be nonnegative and less than the size of the collection. parameter name: index [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Guys I really need for your help. I am getting an error for the above title. All trying to do is when datagridview is click it should display selected record into the text box as well as open it new form. Here is my code.
using System.Data;
using System.Data.SqlClient;
namespace DataGridview
{
public partial class FrmDataGrid : Form
{
SqlConnection con = new SqlConnection("ConnectionString");
public FrmDataGrid()
{
InitializeComponent();
}
private void FrmDataGrid_Load(object sender, EventArgs e)
{
try {
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM UserData";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
try
{
textBox1.Text = dataGridView1.SelectedRows[0].Cells["UserID"].Value.ToString();
textBox2.Text = dataGridView1.SelectedRows[1].Cells["FullName"].Value.ToString();
textBox3.Text = dataGridView1.SelectedRows[2].Cells["Username"].Value.ToString();
textBox4.Text = dataGridView1.SelectedRows[3].Cells["UserPassword"].Value.ToString();
textBox5.Text = dataGridView1.SelectedRows[4].Cells["UserRole"].Value.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
FrmUpdate FormUpdate = new FrmUpdate();
FormUpdate.ShowDialog();
}
}
}
You're trying to fetch 5 rows without even checking that 5 rows exists in the result of your db query. I think you're trying to fetch 5 columns of a row, so use the following code for your requirement:
textBox1.Text = dataGridView1.SelectedRows[0].Cells["UserID"].Value.ToString();
textBox2.Text = dataGridView1.SelectedRows[0].Cells["FullName"].Value.ToString();
textBox3.Text = dataGridView1.SelectedRows[0].Cells["Username"].Value.ToString();
textBox4.Text = dataGridView1.SelectedRows[0].Cells["UserPassword"].Value.ToString();
textBox5.Text = dataGridView1.SelectedRows[0].Cells["UserRole"].Value.ToString();
You are most likely selecting a row that is out of range, during one of these calls:
dataGridView1.SelectedRows[0]

Copying data from Table on MySQL Server to the local MySql Database table in C#

I'm trying to copy some columns from one table located on the MySQL Server, to a local MySQL database using C#. How can I do it ?
I'm not sure if i should use MySQLDataAdapter or Bulk Copy (which i don't understand much).
Bulk copy would be an easier method. It takes a previously created datatable and uploads it to the database.
You can use the following code to help. The below code allows you to return a datatable:
public DataTable FillTable(string sql)
{
MySqlCommand sqlQuery = new MySqlCommand();
cmd.Connection = ;// insert the connection details of the DB to transfer FROM
cmd.CommandText = sql;
DataTable dt = new DataTable();
try
{
conn.Open();
dt.Load(cmd.ExecuteReader());
conn.Close();
}
catch (SqlException ex)
{
Console.WriteLine("fillTable: "+ ex.Message);
}
return dt;
}
Then using the following you can send the data over to your new database:
Datatable newTable = FillTable("SELECT * FROM MyOldTable");
using (MySqlBulkCopy destination = new MySqlBulkCopy("MyNewDatabaseConnection String"))
{
destination.DestinationTableName = "myNewTable";
try
{
destination.WriteToServer(newTable);
}
catch (Exception Ex)
{
Console.WriteLine(Ex.Message);
}
}
Reference MSDN

DataBase not updating in C# when using service-based database

I'm trying to write a program for my exam. I have to use and work with database, i've written some code and encountered a problem. Database is not updating. When I execute my application everything works perfectly, I add some info and it appears in my dataGridView, but when I go to the table in database that i've been updating and select "Show table data", it's empty.
This is function that inserts data into my database.
static public void Insert(string _imonesPav, string _imonesKodas, string _sutartiesPradzia, string _sutartiesPabaiga)
{
try
{
connection.Open();
SqlCommand command = new SqlCommand("INSERT INTO Tiekejai VALUES (#ImonesPav, #ImonesKodas, #SutartiesPradzia, #SutartiesPabaiga)", connection);
command.Parameters.AddWithValue("#ImonesPav", _imonesPav);
command.Parameters.AddWithValue("#ImonesKodas", _imonesKodas);
command.Parameters.AddWithValue("#SutartiesPradzia", _sutartiesPradzia);
command.Parameters.AddWithValue("#SutartiesPabaiga", _sutartiesPabaiga);
command.ExecuteNonQuery();
}
catch (SqlException exception)
{
MessageBox.Show(exception.ToString());
}
finally
{
connection.Close();
}
}
And this is how i use it.
private void updateDatabase_Click(object sender, EventArgs e)
{
SQLFunkcijos.Insert(imonesVardas.Text, imonesKodas.Text, sutartPradzia.Text, sutartPabaiga.Text);
SQLFunkcijos.Refresh(this.dataGridView1);
}
Refresh function is used to update data in dataGridView in real time.
So the question would be, what am i doing wrnog? Why table in database is not updating.
P.S. I've tried all Copy to Output options, it works only with Copy if newer.
EDIT1: Refersh function.
static public void Refresh(DataGridView _dataGridView)
{
try
{
connection.Open();
SqlDataAdapter dataAdapter = new SqlDataAdapter("Select * FROM Tiekejai", connection);
DataTable dataTable = new DataTable();
dataAdapter.Fill(dataTable);
_dataGridView.DataSource = dataTable;
}
catch(SqlException exception)
{
MessageBox.Show(exception.ToString());
}
finally
{
connection.Close();
}
}
EDIT2: Problem solved! I was just using bad connection string. Thanks for everyones help.

One instance of class can't do two functions at the same time

I'm working on ASP.NET web aplication. I'm making it to be MVC and i have a problem with controler class. When i want to populate a drop down list with elements from my SQL database, class is working but only one instance of class with just one drop down list. When i use one class to populate 2 or more drop down lists it doesn't work and VS is not raising any errors. The controler class works, and it can populate drop down list, but just only one drop down list when page is loaded. So to work i have to make an instance of controler class for every drop down list. Please, can someone explain to me why won't work...
this is my DbBroker:
public DataTable VratiKategorije()
{
DataTable kategorije = new DataTable();
using (cn)
{
try
{
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM KategorijaLeka", cn);
adapter.Fill(kategorije);
}
catch (Exception err)
{
//
}
}
return kategorije;
}
And this is my Controler class which is using DbBroker:
public void VratiKategorije(DropDownList ddlKategorije){
try
{
ddlKategorije.DataSource = dbB.VratiKategorije();
ddlKategorije.DataTextField = "Naziv";
ddlKategorije.DataValueField = "ID_Kategorije";
ddlKategorije.DataBind();
}
catch (Exception err)
{
//Handle the err
}
ddlKategorije.Items.Insert(0, new ListItem("", ""));
}
And this is on Load_Page():
protected void Page_Load(object sender, EventArgs e)
{
KontrolerLeka kl = new KontrolerLeka();
kl.VratiKategorije(kategorijaLeka);
}
I found what was the problem. Ok, so when you use using() function with adapter there is no closing connection to database so when same function is called it can't populate another drop down list because the connection from last drop down list function is not closed. Insted of that code in DbBroker i used this and it worked! "pokreniDBtransakciju()" use cn (sql connection string) and after taking Record Set from database the connection is closed using "cn.Close()" which solved all problems.
DbBroker:
public DataTable Uzmi()
{
DataTable dt;
dt = new DataTable();
SqlCommand sc = new SqlCommand();
try
{
sc.CommandText = "SELECT * FROM KategorijaLeka";
sc.Connection = pokreniDBTransakciju();
SqlDataReader reader;
reader = sc.ExecuteReader();
dt.Load(reader);
cn.Close();
}
catch (SqlException e)
{
Console.WriteLine("GRESKA!!!" + e);
return null;
}
return dt;
}

using datagridView, exception occurs for INSERT command, while deletion and updation are working perfectly in C# using access database [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Using save button in my form, all operations (update, delete) working perfectly except INSERT command. Help me out of this...
public partial class frmeditaccess : Form
{
string table = "master";
OleDbDataAdapter da;
OleDbCommandBuilder cb;
DataTable dt;
OleDbConnection conn;
string query;
public frmeditaccess()
{
InitializeComponent();
}
private void btload_Click(object sender, EventArgs e)
{
try
{
conn = new OleDbConnection();
conn.ConnectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;" + #"Data source= C:\Users\ViPuL\Documents\Visual Studio 2010\Projects\feedback#MERI\feedback#MERI\bin\feedback.mdb";
query = string.Format("SELECT * FROM {0}", table);
da = new OleDbDataAdapter(query, conn);
dt = new DataTable();
dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show("Failed due to " + ex.Message);
}
}
private void btsave_Click(object sender, EventArgs e)
{
try
{
cb = new OleDbCommandBuilder(da);
da.Update(dt); //here update, delete are working. Only, Insert throws exception of syntax error in INSERT command.
}
catch (Exception ex)
{
MessageBox.Show("Failed due to " + ex.Message);
}
}
This may be due to reserved keyword used as column name, try by specifying QuotePrefix and QuoteSuffix as below
cb = new OleDbCommandBuilder(da);
cb.QuotePrefix = "[";
cb.QuoteSuffix = "]";
da.Update(dt);

Categories