Hi guys,
Basically I have a drop down list which is populated with text and values depending on a selected index of a radio button list. The code is as follows:
protected void RBLGender_SelectedIndexChanged(object sender, EventArgs e)
{
DDLTrous.Items.Clear();
DDLShoes.Items.Clear();
if (RBLGender.SelectedValue.Equals("Male"))
{
String[] MaleTrouserText = {"[N/A]", "28\"", "30\"", "32\"", "34\"", "36\"", "38\"", "40\"", "42\"", "44\"", "48\""};
String[] MaleTrouserValue = { null, "28\"", "30\"", "32\"", "34\"", "36\"", "38\"", "40\"", "42\"", "44\"", "48\"" };
String[] MaleShoeText = { "[N/A]", "38M", "39M", "40M", "41M", "42M", "43M", "44M", "45M", "46M", "47M" };
String[] MaleShoeValue = { null, "38M", "39M", "40M", "41M", "42M", "43M", "44M", "45M", "46M", "47M" };
for (int i = 0; i < MaleTrouserText.Length; i++)
{
ListItem MaleList = new ListItem(MaleTrouserText[i], MaleTrouserValue[i]);
DDLTrous.Items.Add(MaleList);
}
for (int i = 0; i < MaleShoeText.Length; i++)
{
ListItem MaleShoeList = new ListItem(MaleShoeText[i], MaleShoeValue[i]);
DDLShoes.Items.Add(MaleShoeList);
}
}
else if (RBLGender.SelectedValue.Equals("Female"))`
When the text '[N/A]' is selected I want the value NULL to be inserted into the database. At present when '[N/A]' is selected the value entered into the database is [N/A] does anyone know why?
This is my DB insertion code:
protected void UpdateWorkWear()
{
try
{
string connectionString = Common.GetConnectionString("w34to4tmConnectionString");
SqlConnection conn = new SqlConnection(connectionString);
StringBuilder sbSQL = new StringBuilder();
string str = DDLSurname.SelectedValue;
if (str.Contains("'"))
{
str = str.Replace("'", " ");
}
sbSQL.Append("IF EXISTS (select * from TO4_WORKWEAR_DISTRIBUTION where EmpID = @EmpID)");
sbSQL.Append(" UPDATE TO4_WORKWEAR_DISTRIBUTION SET ");
sbSQL.Append("costCentre = coalesce(@Cost_Centre, CostCentre), EmployeeType = coalesce(@EmployeeType, EmployeeType), Initials = coalesce(@Initials, Initials), Surname = coalesce(@Surname, Surname), ");
sbSQL.Append("IssueQuarter = coalesce(@IssueQuarter, IssueQuarter), TrouserSize = coalesce(@Trouser_S, TrouserSize), TrouserLength = coalesce(@Trouser_L, TrouserLength), ");
sbSQL.Append("PoloSize = coalesce(@Polo_S, PoloSize), SweatSize = coalesce(@Sweat_S,SweatSize), ShortSize = coalesce(@Short_S, ShortSize), ShoeSize = coalesce(@Shoe_S, ShoeSize) WHERE EmpID = @EmpID ");
sbSQL.Append(" else INSERT INTO TO4_WORKWEAR_DISTRIBUTION (EmpID,CostCentre, EmployeeType, Initials, Surname, IssueQuarter, TrouserSize, TrouserLength, PoloSize, SweatSize, ShortSize, ShoeSize) ");
sbSQL.Append("VALUES (@EmpID, @Cost_Centre, @EmployeeType, @Initials, @Surname, @IssueQuarter, @Trouser_S, @Trouser_L, @Polo_S, @Sweat_S, @Short_S, @Shoe_S)");
sbSQL.Replace("@EmpID", "'" + DDLEmpID.SelectedValue + "'");
sbSQL.Replace("@Cost_Centre", "'" + DDLCostCentre.SelectedValue + "'");
sbSQL.Replace("@EmployeeType", "'" + RBLAssociate.SelectedValue + "'");
sbSQL.Replace("@Initials", "'" + DDLInitials.SelectedValue + "'");
sbSQL.Replace("@Surname", "'" + str + "'");
sbSQL.Replace("@Trouser_S", "'" + DDLTrous.SelectedValue + "'");
sbSQL.Replace("@Trouser_L", "'" + DDLTrouserLnh.SelectedValue + "'");
sbSQL.Replace("@Trouser_Q", "'" + DDLTrouserQty.SelectedValue + "'");
sbSQL.Replace("@Polo_S", "'" + DDLPolo.SelectedValue + "'");
sbSQL.Replace("@Polo_Q", "'" + DDLPoloQty.SelectedValue + "'");
sbSQL.Replace("@Sweat_S", "'" + DDLSweat.SelectedValue + "'");
sbSQL.Replace("@Sweat_Q", "'" + DDLSweatQty.SelectedValue + "'");
sbSQL.Replace("@Shoe_S", "'" + DDLShoes.SelectedValue + "'");
sbSQL.Replace("@Short_S", "'" + DDLShor.SelectedValue + "'");
sbSQL.Replace("@InputDate", "'" + System.DateTime.Now.Date.ToString("MM/dd/yyyy") + "'");
sbSQL.Replace("@IssueQuarter", "'" + RBLQuarter.SelectedValue + "'");
SqlCommand cmd = new SqlCommand(sbSQL.ToString(), conn);
cmd.CommandType = CommandType.Text;
conn.Open();
int result = cmd.ExecuteNonQuery(); //execute the SQL command on the database
LiteralControl lit = new LiteralControl();
lit.Text = String.Format("Data Uploaded Successfully.");
Thanks for all your help guys. I have decided to insert blank strings "" into the datbase instead of null now as it makes my life slightly easier when i run reports from it.