Reputation: 1
I`m making a mumbai local train timings project as my class project.. how can i attach my datasource from the database inside a gridview??
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("Select * from @d where(Select * from @c where Source = @a AND Destination = @b)",con);
cmd.Parameters.AddWithValue("@d", DropDownList3.SelectedValue);
cmd.Parameters.AddWithValue("@c",DropDownList3.SelectedValue);
cmd.Parameters.AddWithValue("@a",DropDownList1.SelectedValue);
cmd.Parameters.AddWithValue("@b",DropDownList2.SelectedValue);
//int i = cmd.ExecuteNonQuery();
//if (i > 0)
//{
GridView1.DataSource = //what shud i put here in as a datasource??
GridView1.DataBind();
//}
}
Upvotes: 0
Views: 529
Reputation: 6130
Just simply do some thing like this:
con.Open();
SqlCommand cmd = new SqlCommand("Select * from @d where(Select * from @c where Source = @a AND Destination = @b)",con);
cmd.Parameters.AddWithValue("@d", DropDownList3.SelectedValue);
cmd.Parameters.AddWithValue("@c",DropDownList3.SelectedValue);
cmd.Parameters.AddWithValue("@a",DropDownList1.SelectedValue);
cmd.Parameters.AddWithValue("@b",DropDownList2.SelectedValue);
SqlDataAdapter adapt = new SqlDataAdapter();
DataTable dt = new DataTable();
adapt.SelectCommand = cmd;
adapt.Fill(dt);
GridView GridView1 = new GridView();
GridView1.DataSource = dt;
GridView1.DataBind();
Then this link might be helpful to you : The C# Station ADO.NET Tutorial
Best Regards
Upvotes: 1
Reputation: 15851
You need to Use either SQlDataReader
or SqlDataadpter/Dataset
.
using(SqlConnection con = new SqlConnection(connstring))
{
con.Open();
using(SqlCommand cmd = new SqlCommand("yourQuery",con))
{
cmd = new SqlCommand("Select * from @d where(Select * from @c where Source = @a AND Destination = @b)",con);
cmd.Parameters.AddWithValue("@d", DropDownList3.SelectedValue);
cmd.Parameters.AddWithValue("@c",DropDownList3.SelectedValue);
cmd.Parameters.AddWithValue("@a",DropDownList1.SelectedValue);
cmd.Parameters.AddWithValue("@b",DropDownList2.SelectedValue);
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
Dataset dstList= new Dataset ();
adapter.Fill(dstList);
GridView1.DataSource = dstList;
GridView1.DataBind();
}
}
}
Upvotes: 0