Binding DropDownList from DataSet:
//Getting Dataset from by calling GetAllCountries function
DataSet countryListDs = GetAllCountries();
BindDataSetToDropDown(countryListDs, ddlCountry, "cty_code", "cty_code");
//Common function for DropDownList DataBinding
private void BindDataSetToDropDown(DataSet ds, DropDownList ddl, string textFieldName, string valueFieldName)
{
DataRow row = ds.Tables[0].NewRow();
row[0] = "---Select---";
ds.Tables[0].Rows.InsertAt(row, 0);
ddl.DataSource = ds;
ddl.DataTextField = textFieldName;
ddl.DataValueField = valueFieldName;
ddl.DataBind();
}
//Getting DataSet from SQL Query
private DataSet GetAllCountries()
{
string sqlToExecute = "select * from co_countries FOR XML RAW,ELEMENTS XSINIL, ROOT('Countries')";
con.Open();
cmd.CommandText =
cmd.Connection = con;
XmlReader ExcuteReader = cmd.ExecuteXmlReader();
con.close();
ds.ReadXml(ExcuteReader);
}
Binding DropDownList from DataView:
//Getting Dataset from by calling GetAllStates() function
DataSet statesListDs = GetAllStates();
//Bind US Country states
DataView UsCountriesdv =GetDataView(statesListDs ,"adr_country='UNITED STATES'");
BindToCountriesddl(UsCountriesdv , ddlUS, "sta_name", "sta_code");
//Bind Japan Countries states
DataSet JapanCountriesdv = GetDataView(statesListDs , "adr_country='Japan'");
BindToCountriesddl(JapanCountriesdv , ddlJapan, "sta_name", "sta_code");
//Getting DataView from the DataSet
private DataView GetDataView(DataSet ds, string filterexpression)
{
DataView dv = ds.Tables[0].DefaultView;
dv.RowFilter = filterexpression;
DataRowView newRow = dv.AddNew();
newRow[0] = "---Select---";
dv.Table.Rows.Add(newRow);
return dv;
}
//Common function for DropDownList DataBinding
private void BindToCountriesddl(DataView dv, DropDownList dropdown, string ddlTextField, string ddlValueField)
{
dropdown.DataSource = dv;
dropdown.DataTextField = ddlTextField;
dropdown.DataValueField = ddlValueField;
dropdown.DataBind();
}
cmd.Connection = con;
cmd.Connection = con;
//Getting Dataset from by calling GetAllCountries function
DataSet countryListDs = GetAllCountries();
BindDataSetToDropDown(countryListDs, ddlCountry, "cty_code", "cty_code");
//Common function for DropDownList DataBinding
private void BindDataSetToDropDown(DataSet ds, DropDownList ddl, string textFieldName, string valueFieldName)
{
DataRow row = ds.Tables[0].NewRow();
row[0] = "---Select---";
ds.Tables[0].Rows.InsertAt(row, 0);
ddl.DataSource = ds;
ddl.DataTextField = textFieldName;
ddl.DataValueField = valueFieldName;
ddl.DataBind();
}
//Getting DataSet from SQL Query
private DataSet GetAllCountries()
{
string sqlToExecute = "select * from co_countries FOR XML RAW,ELEMENTS XSINIL, ROOT('Countries')";
con.Open();
cmd.CommandText =
sqlToExecute
;
cmd.Connection = con;
XmlReader ExcuteReader = cmd.ExecuteXmlReader();
con.close();
ds.ReadXml(ExcuteReader);
return ds;
}
Binding DropDownList from DataView:
//Getting Dataset from by calling GetAllStates() function
DataSet statesListDs = GetAllStates();
//Bind US Country states
DataView UsCountriesdv =GetDataView(statesListDs ,"adr_country='UNITED STATES'");
BindToCountriesddl(UsCountriesdv , ddlUS, "sta_name", "sta_code");
//Bind Japan Countries states
DataSet JapanCountriesdv = GetDataView(statesListDs , "adr_country='Japan'");
BindToCountriesddl(JapanCountriesdv , ddlJapan, "sta_name", "sta_code");
//Getting DataView from the DataSet
private DataView GetDataView(DataSet ds, string filterexpression)
{
DataView dv = ds.Tables[0].DefaultView;
dv.RowFilter = filterexpression;
DataRowView newRow = dv.AddNew();
newRow[0] = "---Select---";
dv.Table.Rows.Add(newRow);
return dv;
}
//Common function for DropDownList DataBinding
private void BindToCountriesddl(DataView dv, DropDownList dropdown, string ddlTextField, string ddlValueField)
{
dropdown.DataSource = dv;
dropdown.DataTextField = ddlTextField;
dropdown.DataValueField = ddlValueField;
dropdown.DataBind();
}
//Getting DataSet from SQL Server
private DataSet GetAllStates()
{
string sqlToExecute = "select * from co_countries FOR XML RAW,ELEMENTS XSINIL, ROOT('States')";
con.Open();
cmd.CommandText =
sqlToExecute
;
cmd.Connection = con;
XmlReader ExcuteReader = cmd.ExecuteXmlReader();
con.close();
ds.ReadXml(ExcuteReader);
return ds;
}
Binding Checkbox List from DataView
The following code will bind the checkboxes which are based on the ui_question_id
//Getting UI DataSet by calling GetUIDisplayInfo() function
DataSet uiDs = GetUIDisplayInfo();
//Getting DataView from DataSet by calling GetDataView() Fuction
//Binding Educational Degree UI Checkboxes
DataView educationalDegreeDv = GetDataView(uiDs, string.Format("dmu_question_id={0}", 3));
BindToCheckBoxList(educationalDegreeDv, chlEducationalDegree, "dmu_ui_option", "dmu_ui_dbfield");
//Binding What are your reasons UI Checkboxes
DataView reasonsBIWRIMSdv = GetDataView(uiDs, string.Format("dmu_question_id={0}", 1));
BindToCheckBoxList(reasonsBIWRIMSdv, chlReasonsBIWRIMS, "dmu_ui_option", "dmu_ui_dbfield");
//Binding Professional Affiliations UI Checkboxes
DataView professionalAffDv = GetDataView(uiDs, string.Format("dmu_question_id={0}", 5));
BindToCheckBoxList(professionalAffDv, chlProfessionalAff, "dmu_ui_option", "dmu_ui_dbfield");
//Common function for CheckBoxList DataBinding
private void BindToCheckBoxList(DataView dv, CheckBoxList chl, string chlTextField, string chlValueField)
{
chl.DataSource = dv;
chl.DataTextField = chlTextField;
chl.DataValueField = chlValueField;
chl.DataBind();
}
//Getting Dataset From SQL Query
private DataSet GetUIDisplayInfo()
{
string sqlToExecute = "SELECT * FROM ui_display_ref FOR XML AUTO, ELEMENTS XSINIL, ROOT('ui_display_ref')";
con.Open();
cmd.CommandText =
sqlToExecute
;
cmd.Connection = con;
XmlReader ExcuteReader = cmd.ExecuteXmlReader();
con.close();
ds.ReadXml(ExcuteReader);
return ds;
}
No comments:
Post a Comment