Hello everyone,
I'm new here and also new to ASP.NET. I need some help with the data populating onto the form, I have a web form that has a dropdownlist something like ("Red, White, Blue,") and a few empty checkboxes corresponding to those dropdownlist, if user selects "Red" from the dropdownlist from the form, It makes connection to the db and read the field named Color Where Color = "Red" and populate either a check or not check onto the form. Can someone guide me to the right direction ? Below are my 2 functions "GetColors and SetDefaultColor" onto a form in the Page_Load. it's not working, So how do you append the variable from the db to the (form) or corresponding to the ControlID on the web Form, Thanks
Here is my read GetColor and SetColor to the form
public void GetColors(System.Web.UI.HtmlControls.HtmlSelect ctrl, string strColor)
{
string mySelectQuery, myConnString;
OleDbConnection myConnection;
OleDbCommand myCommand;
OleDbDataReader myReader;
string dbColor = ConfigurationManager.AppSettings.Get("pathDbColor") ;
string retVal;
try
{
mySelectQuery = "SELECT DISTINCT Color FROM ChoiceOfColors WHERE COLOR = '" + strColor+ "'";
myConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbColor;
myConnection = new OleDbConnection(myConnString);
myCommand = new OleDbCommand(mySelectQuery, myConnection);
myConnection.Open();
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
ctrl.Items.Add(myReader["Color"].ToString());
}
}
catch (Exception e)
{
retVal = ("Error: " + e.Message);
}
}
public void SetDefaultColors(string strStandard, ref string [] refCheck, ref string [] refNum)
{
string mySelectQuery, myConnString;
OleDbConnection myConnection;
OleDbCommand myCommand;
OleDbDataReader myReader;
string dbColor = ConfigurationManager.AppSettings.Get("pathDbColor") ;
string retVal;
try
{
mySelectQuery = "SELECT DISTINCT Color FROM ChoiceOfColors WHERE COLOR = '" + strColor+ "'";
myConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbColor;
myConnection = new OleDbConnection(myConnString);
myCommand = new OleDbCommand(mySelectQuery, myConnection);
myConnection.Open();
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
refCheck[Constants.MixRed] = myReader["white"].ToString().Trim(); //will return whether true or false, Checked or unchecked
refNum[Constants.MixRed] = myReader["white_amount"].ToString() + ""; //will return the interger of the amount of white added
}
}
myReader.Close();
myConnection.Open();
myConnection.Close();
}
catch (Exception e)
{
retVal = ("Error: " + e.Message);
}
And in the Page_Load, wants to the checkbox and number populated on the form corresponding to the selection from the dropdownlist
private void Page_Load(object sender, System.EventArgs e)
{
//NOT SURE HOW TO APPROACH THIS, NEED HELP FROM HERE //
if (dbStd.Items.Count > 0)
dbStd.SelectedIndex = 0;
for (int i = 0; i < dbStd.Items.Count; i++)
{
GetColors(dbStd, color.Value);
Rd.Checked = SetDefaultColors(refCheck[Constants.MixRed]);
RdNum.Value = SetDefaultColors(refCheck[Constants.MixRed]]);
}
}
| }