Gridview showing data - c#

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.

Related

Datagridview autoupdate from database everytime open that form

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.

How to prevent my gridview from creating additional rows in c#?

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.

OleDbCommand select does not return expected rows

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

Populate Devexpress GridView using a DataAdapter and SqlCommand in code

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

Delete a record successful, but it delete all records in the database in windows forms

i have a problem. This is happen when i tried to delete a single record in the database when the program runs. The delete record is working, but it is delete all record in the database. How do i fix that? First of all, i view data in my database using datagridview, and once the user click certain row, and the user click delete, it is supposed to delete a certain row that user has been selected, but it is delete all records.
Here is the code:
private void DeleteRecord(object sender, EventArgs e)
{
if (fifthForm.comboBox1.Text == "English")
{
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "DELETE FROM [SeranneRecord]";
conn.Open();
using (OleDbCommand cmd = new OleDbCommand(query, conn))
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
{
DataTable ds = new DataTable();
dataGridView.Rows.RemoveAt(dataGridView.CurrentRow.Index);
adapter.Update(ds);
dataGridView.DataSource = ds;
cmd.ExecuteNonQuery();
System.Media.SoundPlayer sound =
new System.Media.SoundPlayer(
#"C:\Windows\Media\Windows Notify.wav");
sound.Play();
MessageBox.Show("Deleted Successfully", "Deleted");
}
}
}
}
Thanks, also i want to ask about how to modify the records using datagridview. Thanks
You are removing the data grid row : (dataGridView.Rows.RemoveAt(dataGridView.CurrentRow.Index);) but you are not using any information from that row to create a WHERE clause in your query. You need something like `DELETE FROM tablename WHERE RecordName = 'RecordToDelete'.

Categories