I have a number of xamCombos in use. When I bind my data from DB and for a particular field there is a selectedItem in the combo matching all is fine, it gets selected - when I go to save my data back to DB all in fine if I use the cboSomething.Value property except for...(see problem)
When binding data from DB - I do the following:this.cboConsultant_e.SelectedItem = (int)rdr["Consultant"];this.cboConsultant_e.Value = (int)rdr["Consultant"];Problem: If a DB field has a null value for "Consultant" there is no selectedItem initially when loading data, the combo has all its items but nothing selected, desirable behavior. Its when I then select an item from the combo and try to save. The Value is non existant when I try to access it? And im having trouble with selectedItem when trying to access it instead. The error I get is "No Mapping exists from object type System.Data.DataRowView to a know managed provider native type" I understand how the item can be treated as a datarow, but cant figure out best way to access the selectedItem? Doesnt happen if initially on the first load I have a selectedItem for the combo. Please help! Ive stuffed round for a while on this...if (this.cboConsultant_e.Text != null && this.cboConsultant_e.Text.ToString().Length > 0){command.Parameters.Add(new SqlParameter("@Consultant", this.cboConsultant_e.Value));}else{command.Parameters.Add(new SqlParameter("@Consultant", DBNull.Value));}
This is the binding to the control in my code behind.
private
void BindConsultant(){
try
{
//Ward - using spGetWardLookUp
DataSet dtSet = new DataSet();
command =
new SqlCommand(sql, connection);
connection.Open();
adapter.SelectCommand = command;
adapter.Fill(dtSet,
"Cons");
this.itp_Consultant.DisplayMemberPath = "DESC";
this.itp_Consultant.ValuePath = "ID";
connection.Close();
}
ex)
.Show(ex.Message);
XAML:
>
="itp_Consultant" />
string value = ((System.Data.DataRowView)mycombo.SelectedItem).Row.ItemArray[0
].ToString();
This will work