简体   繁体   English

从对象类型System.Web.UI.WebControls.ListItem到已知托管提供程序本机类型的映射不存在

[英]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 从对象类型System.Web.UI.WebControls.ListItem到已知托管提供程序本机类型的映射不存在

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. 我尝试了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();
        }
    }

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 . 请将它们转换为正确的类型或使用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. 该异常告诉您正在将参数设置为ListItem类型的对象,并且您不可能将其插入数据库。

Break when the exception is thrown (with a debugger attached) and look at the Parameters collection ensuring their values are of the correct type. 在引发异常时(附加了调试器)中断,并查看Parameters集合以确保其值是正确的类型。 For now you're looking for a parameter that has a value of ListItem . 现在,您正在寻找一个具有ListItem值的参数。 But I'd check them all for correctness while there. 但是我在那里检查它们的正确性。

From your edited version of the question, you are trying to send parameter values that cannot be translated when actual SQL command is constructed. 从问题的已编辑版本开始,您尝试发送在构造实际SQL命令时无法转换的参数值。 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). 仔细检查以上各行,然后选择实际值(而不是整个对象,就像SelectedItem一样)。

More general, you should try to separate UI from business logic. 更笼统地说,您应该尝试将UI与业务逻辑分开。 Business logic method should work only with relevant data such as identifiers, names etc. and not UI elements such as ListItem s. 业务逻辑方法仅应与相关数据(例如标识符,名称等)一起使用,而不能与UI元素(如ListItem

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不存在从对象类型System.Web.UI.WebControls.ListItem到具有下拉列表的已知托管提供程序本机类型的映射 - No mapping exists from object type System.Web.UI.WebControls.ListItem to a known managed provider native type with dropdown 从对象类型System.Web.UI.WebControls.GridViewRow到已知托管提供程序本机类型的映射不存在 - No mapping exists from object type System.Web.UI.WebControls.GridViewRow to a known managed provider native type 从对象类型System.Web.UI.WebControls.DropDownList到已知的托管提供程序本机类型没有映射 - No mapping exists from object type System.Web.UI.WebControls.DropDownList to a known managed provider native type 不存在从对象类型 System.Web.UI.WebControls.TextBox 到已知托管提供程序本机类型的映射 - No mapping exists from object type System.Web.UI.WebControls.TextBox to a known managed provider native type 对象类型System.Web.UI.WebControls.ListItem没有映射 - No mapping exists from object type System.Web.UI.WebControls.ListItem System.ArgumentException:“不存在从 object 类型 System.Web.UI.WebControls.TextBox 到已知托管提供程序本机类型的映射。” - System.ArgumentException: 'No mapping exists from object type System.Web.UI.WebControls.TextBox to a known managed provider native type.' 错误:从对象类型System.Web.UI.WebControls.TextBox到已知的托管提供程序本机类型的映射不存在 - ERROR: No mapping exists from object type System.Web.UI.WebControls.TextBox to a known managed provider native type C# 不存在从对象类型 System.Web.UI.WebControls.TextBox 到已知托管提供程序本机类型的映射 - C# No mapping exists from object type System.Web.UI.WebControls.TextBox to a known managed provider native type 无法将类型为“ System.Web.UI.WebControls.ListItem”的对象转换为类型为“ System.IConvertible”的对象 - Unable to cast object of type 'System.Web.UI.WebControls.ListItem' to type 'System.IConvertible' 无法将“System.String”类型的对象转换为“System.Web.UI.WebControls.ListItem”类型 - Unable to cast object of type 'System.String' to type 'System.Web.UI.WebControls.ListItem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM