Rico Brouwer Rico Brouwer - 1 year ago 91
HTML Question

Filling a gridview with data from database

Im pretty new to programming and i have a problem that i cant figure out.
I have a report screen with 6 textboxes. I check the checkbxes and look for data in a database with and SQL query. When i click on the button to generate the report, it doesnt show any data in the gridview. I just pasted the gridview from my toolbox to the screen and want to fill it with my c# code and SQL command. Am i missing something ?

protected void Button1_Click(object sender, EventArgs e)

connect = new SqlConnection(@"Data Source=LP12;Initial Catalog=SmmsData;Integrated Security=True");

SqlCommand cmd = new SqlCommand();
cmd.Connection = connect;
cmd.CommandText = "SELECT DrukSensor, FlowSensor, TempSensor, KwaliteitSensor, Toerental, OlieVerbruik"
+ " FROM SysteemSensorInfo"
+ " WHERE DrukSensor=@DrukSensor"
+ " AND FlowSensor=@FlowSensor"
+ " AND TempSensor=@TempSensor"
+ " AND KwaliteitSensor=@KwaliteitSensor"
+ " AND Toerental=@Toerental"
+ " AND Olieverbruik=@OlieVerbruik"

cmd.Parameters.Add("@DrukSensor", SqlDbType.Bit);
cmd.Parameters.Add("@FlowSensor", SqlDbType.Bit);
cmd.Parameters.Add("@TempSensor", SqlDbType.Bit);
cmd.Parameters.Add("@KwaliteitSensor", SqlDbType.Bit);
cmd.Parameters.Add("@Toerental", SqlDbType.Bit);
cmd.Parameters.Add("@OlieVerbruik", SqlDbType.Bit);

cmd.Parameters["@DrukSensor"].Value = CheckBoxDruk.Checked;
cmd.Parameters["@FlowSensor"].Value = CheckBoxFlow.Checked;
cmd.Parameters["@TempSensor"].Value = CheckBoxTemp.Checked;
cmd.Parameters["@KwaliteitSensor"].Value = CheckKwaliteit.Checked;
cmd.Parameters["@Toerental"].Value = CheckBoxToerenTal.Checked;
cmd.Parameters["@OlieVerbruik"].Value = CheckBoxOlieVerbruik.Checked;

DataSet ds = new DataSet();
new SqlDataAdapter(cmd).Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.AutoGenerateColumns = true;


Thanks in advance!

Answer Source

You have not binded data to gridview. Bind data to gridview using DataBind

GridView1.DataSource = ds.Tables[0];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download