1
Answer

Display database mysql with DataSet to Datagridview C#

Photo of kevin

kevin

11y
2.6k
1
I have a database access class which is contain INSERT,UPDATE,DELETE and READ. My INSERT,UPDATE, and DELETE is success full. Now i am stuck how to read the database item that i have inserted into database table. Here is what i do :
 
 DBConnect.cs (A database class that created in a separated project but in a same solution)

public void Select()
{
#region fields
string sqlQuery;
#endregion

#region SQL Queries
sqlQuery = "SELECT NoNota, Nama, Tanggal, Tipe, Keterangan FROM master";
#endregion

#region execute
if (this.OpenConnection() == true)
{
cmd = new MySqlCommand(sqlQuery, connect);
cmd.ExecuteNonQuery();
CloseConnection();
}
#endregion

}

 
Here is my form that will consume that Select() function to display a database item. It's a winform named Report.cs (a WinForm that located in another project which is in a same solution)
 Report.cs
private void btn_Tampil_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();

ds = clsDBConnect.Select();

dgv_report.DataSource = ds;

}

What i got is i can't "convert Type  'void' to dataset". How to do the right way? what i missed here?

Answers (1)

1
Photo of Naresh Joshi
NA 631 252.6k 11y
As referring to your "Select" function it doesn't have any return type that the reason you are getting above error. So to resolve this issue you need to fill the data in Dataset and return the same Dataset  and use it as datasource for your reports.

I have made slight change in the your "Select" function to return dataset :-

public DataSet Select()
{
#region fields
string sqlQuery;
DataSet oDataset = new DataSet();


#endregion


#region SQL Queries
sqlQuery = "SELECT NoNota, Nama, Tanggal, Tipe, Keterangan FROM master";
#endregion


#region execute
if (this.OpenConnection() == true)
{
MySqlCommand cmd = new MySqlCommand(sqlQuery, connect);
MySqlDataAdapter myDataAdapter = new MySqlDataAdapter();
myDataAdapter.SelectCommand = mySqlCommand;
myDataAdapter.Fill(oDataset);
myDataAdapter.Dispose(); 
CloseConnection();
return oDataset 
}
#endregion


}
Accepted