Mateusz Mateusz - 9 days ago 5
SQL Question

No mapping exists from object type System.Web.UI.WebControls.ListItem to a known managed provider native type

This is driving me crazy.


No mapping exists from object type System.Web.UI.WebControls.ListItem to a known managed provider native type


When I try to pass data from a listbox into a database through insert into, I get this error. The data in the listbox is extracted from the database as well.
The insert looks like this:

cmd.Parameters.AddWithValue("@driver",DriversJourney.SelectedItem.ToString());


I tried it with selecteditem(), selectedValue.ToString()), selectedIndex, SelectedValue.

public partial class FindDriver : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\RideShare.mdf;Integrated Security=True");//set connection as a variable
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;


protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] == null)
{
Response.Redirect("~/Account/FindDriver.aspx");
}

else
{
labelWelcome.Text = "Welcome " + Session["UserName"] + "!".ToString();
}

}

protected void search(object sender, EventArgs e)
{
con.Open();
cmd.Connection = con;
cmd.CommandText = "SELECT * FROM Driver WHERE City = '" + JourOrigin.SelectedItem + "' ";
dr = cmd.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
Session["city"] = JourOrigin.SelectedItem.ToString();
Response.Redirect("~/Account/FindDriver.aspx");
NoCity.Visible = false;

}
else
{
DriversJourney.Items.Clear();
DriversJourney.Items.Add("No Drivers in selected city, try another city");
NoCity.Visible = true;
NoCity.Text = "No drivers in selected city, please try another city";
}
con.Close();
}

protected void JourneyAdd_Click(object sender, EventArgs e)
{
using(SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\RideShare.mdf;Integrated Security=True"))
{


SqlCommand cmd = new SqlCommand("INSERT INTO Journey(JourneyOrigin, JourneyDestination, Date, Time, PassengerUserName, DriverUserName) VALUES (@origin, @destination, @date, @time, @driver, @passenger)");
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
cmd.Parameters.AddWithValue("@origin", JourOrigin.SelectedItem);
cmd.Parameters.AddWithValue("@destination", JourDestination.SelectedItem);
cmd.Parameters.AddWithValue("@date", Calendar.SelectedDate);
cmd.Parameters.AddWithValue("@time", date.SelectedItem);
cmd.Parameters.AddWithValue("@driver", DriversJourney.SelectedValue);
cmd.Parameters.AddWithValue("@passenger", Session["username"].ToString());
connection.Open();

cmd.ExecuteNonQuery();
connection.Close();
}
}

Answer

These lines of code are part of your problem:

cmd.Parameters.AddWithValue("@origin", JourOrigin.SelectedItem);
cmd.Parameters.AddWithValue("@destination", JourDestination.SelectedItem);
cmd.Parameters.AddWithValue("@time", date.SelectedItem);

Please cast them to the correct type or use SelectedValue.

That exception is telling you that you're setting a parameter to an object of type ListItem, and you can't possibly insert that into the database.

Break when the exception is thrown (with a debugger attached) and look at the Parameters collection ensuring their values are of the correct type. For now you're looking for a parameter that has a value of ListItem. But I'd check them all for correctness while there.

Comments