I'm trying to get my DataGridView updates back to the Mysql database, but it fails.
DataGridView loads my Database:
private void refreshbtn(object sender, EventArgs e)
{
_sqlhost.Open();
mySqlDataAdapter = new MySqlDataAdapter("select * from ou", _sqlhost);
DataSet DS = new DataSet();
mySqlDataAdapter.Fill(DS);
Tabel.DataSource = DS.Tables[0];
_sqlhost.Close();
}
That part of the code is working just fine. The whole database is loadet to the Datagridview.
It's here the problem comes!
private void Tabel_RowValidated(object sender, DataGridViewCellEventArgs e)
{
DataTable changes = ((DataTable)Tabel.DataSource).GetChanges();
if (changes != null)
{
MySqlCommandBuilder mcb = new MySqlCommandBuilder(mySqlDataAdapter);
mySqlDataAdapter.UpdateCommand = mcb.GetUpdateCommand();
mySqlDataAdapter.Update(changes);
((DataTable)Tabel.DataSource).AcceptChanges();
}
}
When I'm editing a Cell and hit the Enter button. This error comes:
Additional information: Creating dynamic SQL for UpdateCommand is not
supported against a SelectCommand that do not return information about
a key column.
Related
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();
}
}
I have a Database with 2 Tables. Now i want to search for Values in my Databases.
I use this GUI so the "User" can give Information to the Textbox and the searched Values should shown in my Datagridview
I use this Code:
private void TB_MSGHeadline_TextChanged(object sender, EventArgs e)
{
try
{
if (TB_MSGHeadline.Text.Equals(""))
{
}
else
{
DataView dv = new DataView(table);
dv.RowFilter = string.Format("MessageHeadline LIKE '%{0}%'", TB_MSGHeadline.Text);
dataGridView1.DataSource = dv;
}
}
catch (Exception i)
{
MessageBox.Show("" + i);
}
}
Fill the DataGridview
private void GetData(string selectCommand)
{
//Creating a new data adapter based on the specified query.
dataAdapter = new SqlDataAdapter(selectCommand, ConnectionString);
//Create a command builder to generate SQL update, insert, and
//delete commands based on selectCommand. These are used to
//update the Database.
SqlCommandBuilder CommandBuilder = new SqlCommandBuilder();
//Populate a new data table and bind it to the BindingSource
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
BindingSource1.DataSource = table;
}
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = BindingSource1;
GetData("SELECT * FROM Tabelle1");
}
(Found it in a video but dont work for my problem)
But i get the Error, that the row "MessageHeadline" wasnt found
So i want to know, how can i make a LIKE search for my SQL Database?
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
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
I am making windows application and am stuck at one place.
My problem is that i want to display record in a DataGridView by selecting a ComboBox item but I do not understand the proper way to do it. Please help me in overcome this problem.
private void grid_Load(object sender, EventArgs e)
{
con = new SqlConnection(constr);
try
{
con.Open();
//this.studTableAdapter.Fill(this.pRJTestDBDataSet.stud);
//above line show error for connection to database
da = new SqlDataAdapter("SELECT stud_no FROM stud", con);
DataTable dt = new DataTable();
da.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "stud_no";
comboBox1.ValueMember = "stud_no";
comboBox1.DataSource = dt;
comboBox1.SelectedIndex = -1;
comboBox1_SelectedIndexChanged(sender, e);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{ con.Close(); }
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.studTableAdapter.Fill(pRJTestDBDataSet.stud);
//above line show error for connection to database
}
i have tried above code but its not working there error like login fail to user
cmd = new SqlCommand("SELECT stud_no FROM stud", con);
da = new SqlDataAdapter(cmd);
da.Fill(dt);
Combobox1.DataSource = dt;
Combobox1.DisplayMember = dt.Columns("Stud_no").ToString;
rebind the DataGrid at each SelectedItemIndex change event of Combo Box by the data you want to bind.
private void button2_Click(object sender, EventArgs e)//button 2 is a show data button
{
if (combo_floor.Text != "")
{
DataSet ds = new DataSet();
string sql = "select floor_id,floor_no,floor_remark,floor_entrydate from Floorinfo where floor_no='"+combo_floor.Text+"'";
ds = c.select_query(sql);
dataGridView1.DataSource = ds.Tables["a"];
combo_floor.Text = "";
}
else
{
showdata();
//showdata()is made for show all data from the given table name
}
}
//connection is in different class so please dont mind