Reuse DataTable in ButtonClick which is already open in form load - c#

I have open VFP dbf file from formload got the data into data table and
this also in gridview, I want to use this data result in another button click
How can I call that same data in button click,I am new to this hope this maybe
very easy for some , Please help me.
private void Form1_Load(object sender, EventArgs e)
{
DataTable YourResultSet = new DataTable();
OleDbConnection yourConnectionHandler = new OleDbConnection(
#"Provider=VFPOLEDB.1;Data Source=I:\\Bestall\\T&AData.dbc");
yourConnectionHandler.Open();
if (yourConnectionHandler.State == ConnectionState.Open)
{
string mySQL = "select * from Findat"; // dbf table name
OleDbCommand MyQuery = new OleDbCommand(mySQL, yourConnectionHandler);
OleDbDataAdapter DA = new OleDbDataAdapter(MyQuery);
DA.Fill(YourResultSet);
dataGridView1.DataSource = YourResultSet.DefaultView;
// yourConnectionHandler.Close();
}
}

Related

How to associate mysql data to DataRepeater controls

I have a DataRepeater template control with 2 textboxes which I want to associate data from a mysql table
When I run this program I get the representation on my DataRepeater the number of rows existing in my mysql table
RESULT
my question is how to associate the data to those textboxes
private void Form1_Load(object sender, EventArgs e)
{
BindLviewData();
}
protected void BindLviewData()
{
System.Data.DataTable dt = new System.Data.DataTable("db.myTable");
MySqlConnection dbConn = new MySqlConnection("Server = localhost; Database = db; Uid = wwww; Pwd = www; ");
dbConn.Open();
string query = string.Format("SELECT Col1,Col2 FROM myTable");
MySqlCommand cmd = new MySqlCommand(query, dbConn);
MySqlDataAdapter returnVal = new MySqlDataAdapter(cmd);
returnVal.Fill(dt);
BindingSource bs = new BindingSource();
bs.DataSource = dt;
dataRepeater1.DataSource = bs;
dbConn.Close();
}

Refreshing multiple datagridviews with single button click

Not sure if this question has been asked before, so here goes..
I have a front end app coded in C# windows forms. On a second form i have two datagridviews that gets populated from two different sql server pre-defined views
I need to refresh both datagrids at the same time with a single button click
My button click even looks like this..
private void RefreshBTN_Click(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection("removed for illustration only");
string query = "select * from daily_orders order by orderno DESC";
SqlCommand cmd = new SqlCommand(query, myConnection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
How i understand this is, C# opens new connection, queries the DB and returns by filling datagridview1 with the required data. I would like the same click event to request data from another sql view and populate another datagridview at the same time. Visually both grids are aligned vertically on the same form, one on to of the other.
Many thanks in advance
Move the code for refreshing Grid1 into a separate function. Then copy paste and duplicate that function for Grid2. Change the SQL for Grid2 as well as the Grid2 name. Rename the copied function with 2. Then add a call to both functions so your button click will refresh both grids.
private void RefreshBTN_Click(object sender, EventArgs e)
{
//call both functions to refresh both on button click
RefreshGrid1();
RefreshGrid2();
}
private void RefreshGrid1()
{
SqlConnection myConnection = new SqlConnection("removed for illustration only");
string query = "select * from daily_orders order by orderno DESC";
SqlCommand cmd = new SqlCommand(query, myConnection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
//give this function a unique name to represent the second grid refresh
private void RefreshGrid2()
{
SqlConnection myConnection = new SqlConnection("removed for illustration only");
string query = "select * from daily_orders order by orderno DESC";
SqlCommand cmd = new SqlCommand(query, myConnection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
//rename this to your second grid name
dataGridView2.DataSource = dt;
}

Fill and Refresh Datagridview upon button click

I upon button click i retrieve and place data onto a datagridview. However, after i change the "Location", the location and execute button click i receive an error. Below is the code i have and the error i receive.
Code:
private void button1_Click(object sender, EventArgs e)
{ UpdateDG(); }
private void UpdateDG()
{
Con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\\DbFile\\MasterData.accdb");
Con.Open();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter();
da = new OleDbDataAdapter("Select Inc1, Inc2 from MTable where Loc='" + Location+ "'", Con);
da.Fill(dt);
Dg.DataSource = dt;
Dg.Columns[0].HeaderText = "IncTest1";
Dg.Columns[1].HeaderText = "IncTest2";
Con.Close();
Error:
The Microsoft Access database engine cannot find the input table or
query 'MasterTable'.

Datagridview records in CrystalReport

I am trying to print the records from datagridview in CrystalReport.
I have the following code to pouplate the datagridview.
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\dbaza2.mdb");
DataTable dataT;
private void button1_Click(object sender, EventArgs e)
{
dataT = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter();
OleDbCommand com = new OleDbCommand("SELECT * FROM Table1, Table2 WHERE Table1.SifraP = Table2.SifraM AND Table2.Mesec = #Mesec AND Table1.Fakultet = #Fakultet ORDER BY Table.Zvawe", con);
da = new OleDbDataAdapter(com);
com.Parameters.AddWithValue("#Mesec", comboBox1.Text);
com.Parameters.AddWithValue("#Fakultet", comboBox2.Text);
OleDbCommandBuilder builder = new OleDbCommandBuilder(da);
da.Fill(dataT);
this.dataGridView1.DataSource = dataT;
}
And to print the crystal report I have the following code
protected PoFakultetForm izvestaj = new PoFakultetForm();
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
PoFakultetReport raporti = new PoFakultetReport();
raporti.SetDataSource(dataT);
izvestaj.reportSource(raporti);
izvestaj.Show();
}
The problem is that the Datagridview is populated correctly but in the CrystalReport the data is duplicated 4x times.
How can I set the same data from the datagridview to CrystalReport?
In crystal report have parts, like Report Header,Page Header,Details and follows the footer section. If you placed in details section part it will be repeated based on conditional data and if you place at page header then data will be appeared one time but repeated in every page and if you placed in Report Header then it will be shown one time only, So please check where you placed the data fields

updating a database through dataGridView

I have a datagridview in c# and I am filling it with whatever table from my database I want, getting the table name from a textbox. everything is fine until I try to commit the changes I do in the datagridview to the database.
button2 is the button I will be using for update. I've searched all over and apparently updating the adapter should work, but it doesn't in this case and I can't figure out why. here is my code:
public partial class Form1 : Form
{
static string connstr = "DataSource=localhost;Database=sc2db;Trusted_Connection=True;";
SqlConnection conn = new SqlConnection(connstr);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
SqlDataAdapter adap = new SqlDataAdapter();
DataTable dt = new DataTable();
//BindingSource bs = new BindingSource();
SqlCommand comm = new SqlCommand("select * from " + textBox1.Text, conn);
adap.SelectCommand = comm;
dataGridView1.DataSource = dt;
adap.Fill(dt);
}
catch (Exception ex)
{
label1.Text = ex.Message;
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
SqlDataAdapter adap = new SqlDataAdapter();
DataTable dt = new DataTable();
BindingSource bs = new BindingSource();
adap.Update(dt);
}
catch (Exception ex)
{
label1.Text = ex.Message;
}
}
}
Your adap in the button2_Click event handler doesn't have any Command (SelectCommand, DeleteCommand, InsertCommand and UpdateCommand) initialized. So how it can update?
You obviously don't understand how a DataAdapter works. The DataAdapter has 4 Command which are SelectCommand, DeleteCommand, InsertCommand, UpdateCommand. The SelectCommand is required for the method Fill() to work. Other commands are required for the method Update() works, of course in the general case which the DataRows can have all kinds of state: DataRowState.Deleted, DataRowState.Added, DataRowState.Modified. The SelectCommand is also required to build other commands automatically using a CommandBuilder (in SqlClient, that's SqlCommandBuilder). Once you pass a DataTable in the Update() method, all the rows in that table will be browsed through and the state of each DataRow will be checked to see if that row is added, deleted or modified and the adapter will call the corresponding Command (InsertCommand, DeleteCommand and UpdateCommand) it has to perform the actual operation to the database.
In your case, you can try declaring your adap as public and build other commands for it by using SqlCommandBuilder before calling the method Update() like this:
SqlCommandBuilder cb = new SqlCommandBuilder(adap);
Remember that using SqlCommandBuilder() will work only for single table in the Select query, if you have more than 1 table involved you have to build your own Commands for your DataAdapter. That's not difficult, you may want to search more about how to build commands for a DataAdapter.
I hope that helps you get started!
private void button3_Click(object sender, EventArgs e)
{
scb = new SqlCommandBuilder(sqlDataAdapter1);
sqlDataAdapter1.Update(dataSetUser);
}
im able to edit my database from gridview by this code

Categories