I have a search function that I am trying to implement. I use two drop down lists that are populated from my table in sql. I am then trying to display the results in a datagrid. if this is possible.
I have been searching the net for some time now and i cannot find an answer to display data from sql to a datagrid in my c# asp.net web form. I have got a some code but this does not display any thing. The page refreshes and no display of the data.
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection conn = new SqlConnection("workstation id=RIKESH;packet size=4096;user id=sa;data source=RIKESH;persist security info=False;initial catalog=FlyWaySystem");
SqlDataAdapter da = new SqlDataAdapter ("Select * FROM FlightInformation ", conn);
DataSet ds = new DataSet();
da.Fill (ds);
DropDownList1.DataSource = ds;
DropDownList1.DataValueField = "Origin";
DropDownList1.DataBind () ;
string selectedText1=DropDownList1.SelectedItem.Text;
DropDownList2.DataSource = ds;
DropDownList2.DataValueField = "Destination";
DropDownList2.DataBind () ;
string selectedText2=DropDownList2.SelectedItem.Text;
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
string connectionString = "workstation id=RIKESH;packet size=4096;user id=sa;data source=RIKESH;persist security info=False;initial catalog=FlyWaySystem";
string sql = "SELECT * FROM FlightInformation where Origin='selectedText1' and Destination='selectedText2' ";
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
DataSet ds = new DataSet();
connection.Open();
dataadapter.Fill(ds, "FlightInformation");
connection.Close();
DataGrid1.DataSource = ds;
DataGrid1.DataMember = "FlightInformation";
}

New Topic/Question
Reply




MultiQuote





|