Reputation: 29
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();
}
}
Upvotes: 0
Views: 375
Reputation: 7354
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.
Upvotes: 2
Reputation: 23078
From your edited version of the question, you are trying to send parameter values that cannot be translated when actual SQL command is constructed. Or simply, these values do not have simple type as string, int, bool etc.
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);
Double check the lines above and select the actual values (not entire objects, as SelectedItem seems to be).
More general, you should try to separate UI from business logic. Business logic method should work only with relevant data such as identifiers, names etc. and not UI elements such as ListItem
s.
Upvotes: 0