hi matt here again
sorry if the question is a bit vague in discription.
i have data from a table in a datagridview, allready passed from a data base
what i am trying to do is:
1. click an edit button, so the data is copied over into another table.
2. change the values then save it back to the original.
problem on copying over the data to the editing table
For copying over the data, its simply pressing a button
the table name is passed over using a textbox, so i cant figure out the problem as it is the same as typing it manually, as i did above to fetch the table.
the error it is throwing is:
ExecuteReader requires an open and available connection. The connection's current state is closed.
and also.....
Syntax error (missing operator) in query expression '= 10248'.
also why is it closed when i declare the connection as open?, is it closing some where??
if needed i can post the entire operation here..
please help driving me nuts atm...lol
sorry for not being clear enough
the entire form code is as follows
<pre>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.SqlClient;
namespace northwind
{
public partial class MainScreen : Form
{
bool connected = false;
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "E:\\data cent\\Northwind.mdb");
string SelectedLine;
String[] OldValues = new String[20];
String[] DataColumnNames = new String[20];
public MainScreen()
{
InitializeComponent();
}
private void MainScreen_Load(object sender, EventArgs e)
{
hiddenBox.Hide();
editingTableView.Hide();
}
private void connectButton_Click(object sender, EventArgs e)
{
string connectionString = GetConnectionString();
using (OleDbConnection con= new OleDbConnection(connectionString))
{
con.Open();
connectButton.BackColor = Color.Green;
connectButton.Text = "Connected";
connected = true;
}
}
private void logoutButton_Click(object sender, EventArgs e)
{
Form1 fm1 = new Form1();
tableView.DataSource = null;
connected = false;
this.Close();
fm1.Show();
}
private void exitButton_Click(object sender, EventArgs e)
{
tableView.DataSource = null;
connected = false;
MessageBox.Show("Exiting");
Application.Exit();
}
static private string GetConnectionString()
{
return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "E:\\data cent\\Northwind.mdb";
}
private void ordersButton_Click(object sender, EventArgs e)
{
if (connected == false)
{
MessageBox.Show("not connected");
}
if (connected == true)
{
tableSelected.Text = "Orders";
hiddenBox.Show();
editingTableView.Show();
string connectionString = GetConnectionString();
DataSet ReturnedTable = new DataSet();
BindingSource bs = new BindingSource();
string queryString = " SELECT * FROM Orders" + ";";
try
{
OleDbDataAdapter adapter = new OleDbDataAdapter(queryString, connectionString);
adapter.Fill(ReturnedTable, "Returned Table");
bs.DataSource = ReturnedTable.Tables[0];
tableView.DataSource = bs;
tableView.Update();
adapter.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void editButton_Click(object sender, EventArgs e)
{
DataSet ReturnedTable = new DataSet();
BindingSource bs = new BindingSource();
string connectionString = GetConnectionString();
GetColumnNames();
SelectedLine = tableView.SelectedCells[0].Value.ToString();
string queryString = "SELECT * FROM " + tableSelected.Text + " WHERE " + DataColumnNames[0] + "= " + SelectedLine + ";";
try
{
OleDbDataAdapter adapter = new OleDbDataAdapter(queryString, connectionString);
adapter.Fill(ReturnedTable, "Returned Table");
bs.DataSource = ReturnedTable.Tables[0];
editingTableView.DataSource = bs;
editingTableView.Update();
for (int i = 0; i < editingTableView.Rows[0].Cells.Count - 1; i++)
{
OldValues[i] = editingTableView.Rows[0].Cells[i].Value.ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void GetColumnNames()
{
string connectionString = GetConnectionString();
using (OleDbConnection con = new OleDbConnection(connectionString))
{
OleDbCommand command = con.CreateCommand();
command.CommandText = "SELECT column_name AS Col_Name FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '" + tableSelected.Text + "';";
string tempString = null;
try
{
OleDbDataReader columnDr = command.ExecuteReader();
int i = 0;
while (columnDr.Read())
{
DataColumnNames[i] = columnDr.GetString(0);
tempString += DataColumnNames[i].ToString() + " ";
i++;
}
columnDr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}
appologise greatly for pasting the entire form, but its hard to explain without the whole picture, the previouse forms are a login and a splash screen.
thanks all