Dropdown list question
Hi guys, just studying C# as my language of choice for .NET, Iam a Delphi developer trying to catch-up with the .NET fever.
Im working in a datagrid and included one Dropdownlist in my edit template so that user will just have to select item from the list instead of typing it in textbox. I binded my dropdownlist to a maintenance table tblCountry it has two fields country_code and country_name.
How can I get the key field (country_code)of the selected item instead of the list item (which is country_name) for insertion in the Customers table.
Actually i have solved the problem by running separate query on tblCountry (using the country_name selected) then passing the key field to a variable, but I want to know if theres any easier way to deal with this coz that's very easy task in delphi's VCL dropdownlist becoz it has property for key field and display field item.
Thanks for your help.
Regards.
heres my code
public void DataGrid1_Update(Object sender, DataGridCommandEventArgs E)
{
SqlConnection MyCon = new SqlConnection(ConStr);
//Get Input value of lblContNo from the GridEdit Template
string strControl_No = ((Label)(E.Item.FindControl("lblContNo"))).Text;
string strFName=((TextBox) (E.Item.FindControl("txtFName"))).Text;
string strLName=((TextBox) (E.Item.FindControl("txtLName"))).Text;
//Get the Selected Value from the ListBox populated via the BindTheCountry() function.
strCountry= ((DropDownList)E.Item.FindControl("ddlCountry")).S electedItem.ToString();
//find the key field of that selected country string from the table maintenance.
string SqlLookCode="SELECT country_code, Country FROM tblCountry where Country_Name = '"+strCountry+"'";
//execute sqlLookCode
SqlCommand CmdLocCountry = new SqlCommand(SqlLookCode, MyCon);
SqlDataReader myReader;
//open connection
try
{
MyCon.Open();
myReader = CmdLocCountry.ExecuteReader();
myReader.Read();
//assigned query result to Country_Code.
string Country_Code= myReader.GetString(0);
myReader.Close();
//Save the country_code value to employee master file.
string SqlStr= "UPDATE H_EMPMASTER SET First_Name='" + strFName
+"',Last_Name='"+strLName+"',Country='"+Country_Co de+"'WHERE Control_No='" + strControl_No + "'";
SqlCommand SqlCmd = new SqlCommand(SqlStr,MyCon);
//Execute SqlStr.
SqlCmd.ExecuteReader(CommandBehavior.CloseConnecti on);
//Return to normal grid view.
DataGrid1.EditItemIndex = -1;
//re-bind grid to updated table source.
BindGrid();
}
catch (SqlException ex)
{
lblError.Text = "Error: "+ ex.Message;
}
finally
{
MyCon.Close();
}
}
.....
|