I try researching this everywhere but it appears when I run my windows form application and display my database. Each it keeps automatically skipping and adding rows.
Here is a screen shot of my gridview
Here is also my code where it displays the database when I click a button:
try
{
conn = new MySqlConnection();
conn.ConnectionString = connstring;
query = "INSERT INTO schedule(name) VALUES(#namevalue)";
MySqlCommand cmd = new MySqlCommand(query, conn);
cmd.Parameters.AddWithValue("#namevalue", this.nameEmp.Text);
conn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Connection Success");
myadapt = new MySqlDataAdapter();
string sq = "SELECT * FROM schedule";
myadapt.SelectCommand = new MySqlCommand(sq, conn);
tb = new DataTable();
myadapt.Fill(tb);
BindingSource src = new BindingSource();
src.DataSource = tb;
dataGridView1.DataSource = src;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
Do you mean you want to show those rows which name column is not empty?
Gridview can just show the table you query from the DB,so you can change the SQL code to get the right table,
for example:change SELECT * FROM schedule to SELECT * FROM schedule WHERE schedule(name) IS NOT NULL.
actually, I figured it out. I had in that code above the statement
INSERT twice in my program. I removed it and redid my database on my server and fixed the problem. So sorry for the trouble.
Related
While adding creating GUI in VS2019 you can add TableDataAdapter which will add/show Datagrid showing that tables data. with this code in designer this.data1TableAdapter.Fill(this.test1DataSet.data1);
But problem is if there are any changes in database/table they will not be reflected in that datagrid.
I tried using data1DataGridView.Update(); and data1DataGridView.Refresh(); but it literally does nothing.
than I added my database connection string as datasource.(I know thats stupid attempt, But I tried it anyway.) which obviously didn't worked and erased my whatever(non updated) data showing in datagrid.
So here is my question is there any simple way to update that automatically every time there is change in database/table ?
Disclaimer: I have it working but another way. I will share that too.
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
conn.ConnectionString = #"Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\Documents\test1.mdb";
OleDbCommand comm = new OleDbCommand("SELECT * FROM data1;", conn);
conn.Open();
OleDbDataAdapter dap = new OleDbDataAdapter(comm);
DataTable ds = new DataTable();
dap.Fill(ds);
data1DataGridView.DataSource = ds;
comm.ExecuteNonQuery();
conn.Close();
According to me there are two things you can do.
1- create a Refresh button and at that button call the userdefined function refresh
public void Refresh(object sender, EventArgs e)
{
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
conn.ConnectionString = #"Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\Documents\test1.mdb";
OleDbCommand comm = new OleDbCommand("SELECT * FROM data1;", conn);
conn.Open();
OleDbDataAdapter dap = new OleDbDataAdapter(comm);
DataTable ds = new DataTable();
dap.Fill(ds);
data1DataGridView.DataSource = ds;
comm.ExecuteNonQuery();
conn.Close();
}
this function rebind data to your new table every time you clicked
2- you can use timer control to refresh your data grid. I am pasting the link how to use timer control in c# you can follow it, and simply paste the above Refresh function definition in timer control. it will refresh your datagrid according to time interval you provide.
My thinking is that the problem lies with the adapter. However, I'm still very green and would appreciate any help. I've searched this forum and have had no luck with any of the resolutions I've found (pebkac probably). The application is simple, use textbox input in a parameterized select statement to populate the datagridview. The query will execute but the grid remains empty. VS2017, MySql8.0
private void button2_Click(object sender, EventArgs e)
{
string myConnectionstring = null;
string mySelect = "SELECT * FROM part_data WHERE 'SERIAL' = #test; ";
string tb7 = textBox7.Text;
//Set Connection String And Create Connection
myConnectionstring = "server=localhost;user= admin; database= trace;port=3306;password= admin;Allow User Variables=True";
DataSet ds = new DataSet();
using (MySqlConnection myConnection = new MySqlConnection(myConnectionstring))
{ //Create Command Object
//MySqlCommand myCommand = new MySqlCommand(mySelect, myConnection);
try
{
myConnection.Open();
// myCommand.Prepare();
//myCommand.Parameters.AddWithValue("#test",tb7);
MySqlDataAdapter adapter = new MySqlDataAdapter(mySelect, myConnectionstring);
adapter.SelectCommand.Parameters.AddWithValue("#test",tb7);
//debug to see final select statement
richTextBox1.Text = mySelect;
adapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show("Query Failed" + ex);
}
}
At first glance it looks as though your query might be bad. Try using backticks around the column name.
SELECT * FROM part_data WHERE `SERIAL` = #test;
Try making the query without parameters, there is no need to do it like that, then try the DataSourceView of your dataset.
I want to display data into the gridview from the database
But Currently there is no data into the table but it still showing the records from the table.
Below is my code:-
protected void DisplayGrid()
{
OracleCommand cmd = new OracleCommand("Select * from XXACL_PN_FLAT_STATUS_HIS", ObjPriCon);
DataTable dtfillgrid = new DataTable();
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(dtfillgrid);
GrdBookingStatus.DataSource = dtfillgrid;
GrdBookingStatus.DataBind();
}
and on page_load
DisplayGrid();
I tried from here its related to Session but I dont have any session right now.
Also I cross checked the database name, Table name and connection string
UPDATE
I tried with Open and close like below but still it didn't worked
try
{
ObjPriCon.Open();
OracleCommand cmd = new OracleCommand("Select * from XXACL_PN_FLAT_STATUS_HIS", ObjPriCon);
DataTable dtfillgrid = new DataTable();
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(dtfillgrid);
GrdBookingStatus.DataSource = dtfillgrid;
GrdBookingStatus.DataBind();
}
catch (Exception ex)
{
}
finally
{
ObjPriCon.Close();
}
Finally after a research of many things, I manage to got the solution.
Actually in Toad.
After executing the select query in the Oracle, the next step what I thought of was COMMITING after I executed my select query.
So when I checked after commiting, It worked for me.
I have two Access tables, namely Projects, including the rows of projectTitle and partyID, and ProjectParty, including the rows of title and ID.
private void btnSearch_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=HesabKetab.accdb;Persist Security Info=False;";
//search in the database
OleDbCommand oleCmd = new OleDbCommand();
oleCmd.Connection = conn;
if (radioBtnByTitle.Checked)
{
oleCmd.CommandText = "SELECT * FROM Projects WHERE projectTitle=#projectTitle";
oleCmd.Parameters.AddWithValue("#projectTitle", txtProjectTitle.Text);
}
else if (radioBtnByParty.Checked)
{
oleCmd.CommandText = "SELECT * FROM Projects WHERE partyID=#partyID";
oleCmd.Parameters.AddWithValue("#partyID", comboParty.SelectedValue.ToString());
}
//execute query
OleDbDataAdapter ole_da = new OleDbDataAdapter(oleCmd);
DataTable dt= new DataTable();
try
{
conn.Open();
ole_da.Fill(dt);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
dataGridViewDisplaySearchResults.DataSource = dt;
conn.Close();
}
In the above code I am trying to retrieve the values of the Projects Access database table. The second if is successful and it loads the queried rows into DataGridView. But the first if (when true) does not return the expected values. In fact, it loads nothing into the DataGridView. I have no idea why the query does not work when I try to do the select based on projectTitle. I tried debugging but I got no clue which parameters were being passed to the select command. Where am I wrong?
instead txtProjectTitle.ToString() in the first condition, isn't it txtProjectTitle.Text
I have a little problem with populating my DevExpress Gridview, I want to have a two level gridview and using SqlCommand. At first I created a Dataset and added two tables and also defined relation for them. But it does not work. Can you help me find my problem?
Here is my code
string owner = "SELECT [OBJECTID],[Name] ,[Family] ,[Father] ,[Dftarche] ,[Birthday] ,[education] ,[home_address] ,[farm_address] ,[ensurance] ,[phone] ,[home_number] ,[owner_id] FROM [dbo].[OWNER]";
string property = "SELECT [number] ,[owner_ID] ,[GPSId] ,[Energy],[corp_type] ,[Pool],[irrigation] ,[variety] ,[trees] ,[utilizat] ,[address] ,[water_hour] ,[w_source] ,[w_inche],[w_dore],[NoeMalekiat],[MotevasetBardasht],[Area] ,[OBJECTID],[Shape] FROM [dbo].[Property] ";
string strConnString = Properties.Settings.Default.land_gisConnectionString;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
SqlCommand command = new SqlCommand(owner, con);
SqlDataAdapter adapter = new SqlDataAdapter();
System.Data.DataSet dsMain = new System.Data.DataSet();
adapter.SelectCommand = command;
adapter.Fill(dsMain, "First Table");
adapter.SelectCommand.CommandText = property;
adapter.Fill(dsMain, "Second Table");
dsMain.Tables.Add(iFeatureSet.DataTable.Copy());
adapter.Dispose();
command.Dispose();
DataRelation newRelation = new DataRelation("املاک شخصی", dsMain.Tables["First Table"].Columns["owner_id"], dsMain.Tables["Second Table"].Columns["owner_ID"]);
dsMain.Relations.Add(newRelation);
GridAttrebuteTable.DataSource = dsMain.Tables[2];
// gridView5.DataSource = dsMain.Tables[1];
dataGridView1.DataSource = dsMain;
I searched and found this http://msdn.microsoft.com/en-us/library/bh8kx08z.aspx and it seems my code is right but it does not show anything in grids
Thank you very much for your help
I Could figure out how to fix it.It works fine now(Above Code is edited) but now If I add a new DataTable I do not know why it does not work again
You need a new GridView for each detail table. You can't display both a master and detail in the same GridView.
Try this example