Greetings experts,
We have a database called Types that has two fields, TypeID, and Type Name.
Then we have a textbox called newType.
The idea of this textbox is that when a user attempts to select an item from the combobox and that item does not exist in the combobox, then enter the new item into the newType textbox and submit to the database.
Once submitted, it now becomes available for selection in the combobox.
Here is how I have approached this problem:
- //Populate Combobox1
- SqlDataAdapter sda1 = new SqlDataAdapter("SELECT DISTINCT TypeId, TypeName FROM Types", constr);
- //Fill the DataTable with records from Table.
- DataTable dt = new DataTable();
- sda1.Fill(dt);
- //Insert the Default Item to DataTable.
- DataRow row = dt.NewRow();
- row[0] = 0;
- row[1] = "Please select member";
- dt.Rows.InsertAt(row, 0);
- //Assign DataTable as DataSource.
- txtType.DataSource = dt;
- txtType.DisplayMember = "TypeName";
- txtType.ValueMember = "TypeId";
- txtType.SelectedIndex = 0;
Once a value has been entered into the newType textbox field, I process it:
- //check if this is new type and insert into Types table
- if (mID <= 0)
- {
- SqlCommand cmd = new SqlCommand("insert into Types(TypeName) VALUES(@typeName)SELECT SCOPE_IDENTITY()", con);
- cmd.CommandType = CommandType.Text;
- cmd.Parameters.AddWithValue("@typeName", txtTypeName.SelectedItem);
- con.Open();
- //get the newly inserted ID - to be used later
- int typeid = Convert.ToInt32(cmd.ExecuteScalar());
- con.Close();
- if (typeid != 0)
- {
- MessageBox.Show("Record Saved Successfully");
- }
- //insert new typeid along with other fields into Transactions table
- …
- ...
The problem is when I click submit to add the newType to the database, I am getting the following error message:
System.ArgumentException: No mapping exists from object type System.Data.DataRowView to a known managed provider native type.
at System.Data.SqlClient.MetaType.GetMetaTypeFromValue(Type dataType, Object value, Boolean inferLen, Boolean streamAllowed)
at System.Data.SqlClient.MetaType.GetMetaTypeFromValue(Type dataType, Object value, Boolean inferLen, Boolean streamAllowed)
Any ideas what I am doing wrong?
Thanks in advance
Posted May 17, 2019, 7:10 PM
Posted May 15, 2019, 10:20 AM
Hi Rajanikant,
Thanks for your response.
Rajanikant HawaldarPosted May 14, 2019, 9:39 PM
Rafnas T PPosted May 14, 2019, 8:11 PM
Posted May 14, 2019, 2:36 PM
Thank you so very much for your response.
Inititially, I had this:
Amit GuptaPosted May 14, 2019, 12:33 PM
Amit GuptaPosted May 14, 2019, 12:32 PM