Link to home
Start Free TrialLog in
Avatar of dodgerfan
dodgerfanFlag for United States of America

asked on

ASP.Net combobox handle no change for edit

I have an asp.net form (C# code behind). I'm using the combo box control from DevExpress. The combo box shows a list of countries to select from. It's one of several controls on the form, all tied to a database table. When I insert a record, I have no issues. When I bring a record up for editing, if I make a change to the combobox dropdown (and any other edits, it works fine. However, if I bring it up and make edits without making a change to the value in the combobox, I get the "Error converting data type nvarchar to int" error. I am trying to find out how to handle this. The combobox displays the Country that has been selected, but a numeric value is what is stored in the table. Any ideas?
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

ComboBoxes can have two values associated with them, a text value (displayed value) and a valuemember value. Perhaps the combobox is displaying the countries as a text value, but the valuemember is numeric and that's what is saved to the database to a column that only accepts integers. Fixing it should be a matter of changing the code so it writes the valuemember value to the database instead of the text value. Something like ComboBox1.SelectedItem.Value instead of just ComboBox1.SelectedItem. Or, if no changes are made to the combobox selection, skip that field in the update statement.
Avatar of Rick
Rick

You could check if the combobox value or text has been changed before submitting the changes.
Avatar of dodgerfan

ASKER

My code looks like this:
HTML:
<dx:ASPxComboBox ID="cboCountry" runat="server" DatSourceID="Countries" TextField="ContryName" ValueField="CountryID" ValueTupe="System.Int32" Value='<%# Bind("CountryName") %>'>>/dx:ASPxComboBox>
<asp:SqlDataSource ID="Countries" runat="server" ConnectionString="<%$ ConnectionStrings:MyConn %>"</aspSqlDataSource>

The edit and insert are part of separate stored procedures with parameters. I have for the combobox as part of the stored procedure in code behind:
cmd.Parameters.AddWithValue("@CountryID", cboCountry.Value)

I've changed cboCountry.Value to ValueField, too, but that gave me the same error everytime I tired to edit, regardless of changing the country. I have a very similar stored procedure for inserting, and it works fine, but obviously when inserting a new record, a user always has to enter a country. How would you handle checking for a change?
ASKER CERTIFIED SOLUTION
Avatar of Rick
Rick

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I changed the control to an asp:DropDownList from the DevExpress combo box. And now it works perfectly. I have a question into their support to see what I did wrong with that control.
so is it a vendor issue?
closing this. primarily a vendor issue.