pass values to 2010 control
-
I am adding a checkboxlist control to a C# asp.net 2010 since I am new to working with web pages. The following code works if I hardcode the value in the where clause. However, I want to pass the value in the where clause as a paramter. Thus can you tell me what I could change in the code below to pass the value to the where statement not as a hardcoded value? [^]DBConnection.FillAttChkListBox(ChkBoxLstPlan, "Hnumber", "Hnumber", DBConnection.ExecuteQuery("select p.Hnumber from dbo.Org o inner join Pl p on o.OrganizationID =p.OrganizationID where OrganizationName='orgname1'"), false); public static DataTable ExecuteQuery(string SQLstring) { string constr = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString; //string constr = ConfigurationManager.ConnectionStrings["Client_ServiceConnectionString"].ConnectionString; SqlConnection conn = new SqlConnection(constr); DataTable dt = new DataTable("tbl"); using (conn) { conn.Open(); SqlCommand comm = new SqlCommand(SQLstring, conn); comm.CommandTimeout = 0; SqlDataAdapter da = new SqlDataAdapter(comm); da.Fill(dt); } return dt; } public static void FillAttChkListBox(CheckBoxList chklistBox, String dataValueField, string dataTextField, DataTable dataTbl, bool bHasBlank) { chklistBox.DataTextField = dataTextField; chklistBox.DataValueField = dataValueField; chklistBox.DataSource = dataTbl; chklistBox.DataBind(); if (bHasBlank) { chklistBox.Items.Insert(0, new ListItem()); // chklistBox.Items.Insert(1, new ListItem("All")); } }[^]
-
I am adding a checkboxlist control to a C# asp.net 2010 since I am new to working with web pages. The following code works if I hardcode the value in the where clause. However, I want to pass the value in the where clause as a paramter. Thus can you tell me what I could change in the code below to pass the value to the where statement not as a hardcoded value? [^]DBConnection.FillAttChkListBox(ChkBoxLstPlan, "Hnumber", "Hnumber", DBConnection.ExecuteQuery("select p.Hnumber from dbo.Org o inner join Pl p on o.OrganizationID =p.OrganizationID where OrganizationName='orgname1'"), false); public static DataTable ExecuteQuery(string SQLstring) { string constr = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString; //string constr = ConfigurationManager.ConnectionStrings["Client_ServiceConnectionString"].ConnectionString; SqlConnection conn = new SqlConnection(constr); DataTable dt = new DataTable("tbl"); using (conn) { conn.Open(); SqlCommand comm = new SqlCommand(SQLstring, conn); comm.CommandTimeout = 0; SqlDataAdapter da = new SqlDataAdapter(comm); da.Fill(dt); } return dt; } public static void FillAttChkListBox(CheckBoxList chklistBox, String dataValueField, string dataTextField, DataTable dataTbl, bool bHasBlank) { chklistBox.DataTextField = dataTextField; chklistBox.DataValueField = dataValueField; chklistBox.DataSource = dataTbl; chklistBox.DataBind(); if (bHasBlank) { chklistBox.Items.Insert(0, new ListItem()); // chklistBox.Items.Insert(1, new ListItem("All")); } }[^]
You can try to make this select statement in a string variable so you can concatinate your string variable in the where clause Like that
string query="select p.Hnumber from dbo.Org o inner join Pl p on
o.OrganizationID =p.OrganizationID where
OrganizationName='"+AnyStringVariable+"'";so that you can add any value in the where clause. Try This , if it is not working , try to contact me . Good Luck :)
Sincerely, Ibrahim Hebeish | SoftwareDeveloper | Portal Services | EgyptNetwork(Com) M: +201281744594 Email: i.hebeish@egyptnetwork.com
-
You can try to make this select statement in a string variable so you can concatinate your string variable in the where clause Like that
string query="select p.Hnumber from dbo.Org o inner join Pl p on
o.OrganizationID =p.OrganizationID where
OrganizationName='"+AnyStringVariable+"'";so that you can add any value in the where clause. Try This , if it is not working , try to contact me . Good Luck :)
Sincerely, Ibrahim Hebeish | SoftwareDeveloper | Portal Services | EgyptNetwork(Com) M: +201281744594 Email: i.hebeish@egyptnetwork.com
how can
-
how can
DBConnection.ExecuteQuery("select p.Hnumber from dbo.Org o inner join Pl p on o.OrganizationID =p.OrganizationID where OrganizationName='orgname1'")
you can make it like that :
// You Can take a value from any control
// (textbox, dropDownList,.... , etc.)string organizationName=_organizationNameTextBox.Text;
string query="select p.Hnumber from dbo.Org o inner join Pl p on
o.OrganizationID =p.OrganizationID where
OrganizationName='"+organizationName+"'";DBConnection.ExecuteQuery(query)
so you can pass any value to the "Where" Clause (it is simple way). or you can make a Stored-Procedure that makes the same Query & takes a parameter for the Where clause and this is the (recommended solution).
Sincerely, Ibrahim Hebeish | SoftwareDeveloper | Portal Services | EgyptNetwork(Com) M: +201281744594 Email: i.hebeish@egyptnetwork.com