将varchar值'System.Web.UI.WebControls.DropDownList'转换为数据类型int时,转换失败. [英] Conversion failed when converting the varchar value 'System.Web.UI.WebControls.DropDownList' to data type int.

查看:64
本文介绍了将varchar值'System.Web.UI.WebControls.DropDownList'转换为数据类型int时,转换失败.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我收到错误消息将varchar值"System.Web.UI.WebControls.DropDownList"转换为数据类型int时转换失败."当我在下拉列表中输入数值时.谁能告诉我我哪里出了错

代码在这里

aspx

Hi all

I am getting the error "Conversion failed when converting the varchar value ''System.Web.UI.WebControls.DropDownList'' to data type int." when i enter numerical values into the drop down list . can any one tell me where i went wrong

code goes here

aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        #form1
        {
            height: 3px;
            font-weight: 700;
        }
    </style>
</head>
<body bgcolor="#000000">
    <form id="form1" runat="server">
    <br />

    <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem>0</asp:ListItem>
        <asp:ListItem>1</asp:ListItem>
        <asp:ListItem>2</asp:ListItem>
    </asp:DropDownList>
    <br />

    <br />
    <br />
    <asp:DropDownList ID="DropDownList2" runat="server" >
        <asp:ListItem>0</asp:ListItem>
        <asp:ListItem>1</asp:ListItem>
        <asp:ListItem>2</asp:ListItem>

    </asp:DropDownList>
    <br />
    <br />

    <asp:DropDownList ID="DropDownList3" runat="server">
        <asp:ListItem>0</asp:ListItem>
        <asp:ListItem>1</asp:ListItem>
        <asp:ListItem>2</asp:ListItem>

    </asp:DropDownList>
    <br />
    <br />

    <asp:DropDownList ID="DropDownList4" runat="server" AutoPostBack="True">
        <asp:ListItem>0</asp:ListItem>
        <asp:ListItem>1</asp:ListItem>
        <asp:ListItem>2</asp:ListItem>

    </asp:DropDownList>
    <br />
    <br />
    <asp:DropDownList ID="DropDownList5" runat="server">
        <asp:ListItem>0</asp:ListItem>
        <asp:ListItem>1</asp:ListItem>
        <asp:ListItem>2</asp:ListItem>

    </asp:DropDownList>
    <br />
    <br />

    <asp:DropDownList ID="DropDownList6" runat="server">
        <asp:ListItem>0</asp:ListItem>
        <asp:ListItem>1</asp:ListItem>
        <asp:ListItem>2</asp:ListItem>

    </asp:DropDownList>
    <br />
    <br />

    <br />
    <br />
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    </form>
</body>
</html>



aspx.cs



aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;


public partial class Default7 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    SqlConnection con = new SqlConnection("Data Source=ADMIN-PC;Initial Catalog=livemymoments;Integrated Security=True");
    SqlCommand cmd;
    SqlDataReader dr;
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        con.Open();
        cmd = new SqlCommand("insert into marriage_config values('" + DropDownList1 + "','" + DropDownList2 + "','" + DropDownList3 + "','" + DropDownList4 + "','" + DropDownList5 + "','" + DropDownList6 + "')", con);
        cmd.ExecuteNonQuery();

    }
}

推荐答案

您需要执行"DropDownList3.SelectedValue"或"DropDownList3.SelectedIndex"以获取所选值.
You need to do "DropDownList3.SelectedValue" or "DropDownList3.SelectedIndex" to get the selected values.


使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Data;
使用System.Data.Sql;
使用System.Data.SqlClient;
使用System.Data.SqlTypes;


公共局部类Default7:System.Web.UI.Page
{
受保护的void Page_Load(对象发送者,EventArgs e)
{

}
SqlConnection con =新的SqlConnection(数据源= ADMIN-PC;初始目录= livemymoments;集成安全性= True");
SqlCommand cmd;
SqlDataReader dr;

受保护的void Button1_Click(对象发送者,EventArgs e)
{
con.Open();
cmd = new SqlCommand(插入姻缘配置值(""+ DropDownList1.SelectedItem +"''," + DropDownList2.SelectedItem +"," + DropDownList3.SelectedItem +"," + DropDownList4.SelectedItem +'',''" + DropDownList5.SelectedItem +'',""+ DropDownList6.SelectedItem +"''),con);
cmd.ExecuteNonQuery();

}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;


public partial class Default7 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
SqlConnection con = new SqlConnection("Data Source=ADMIN-PC;Initial Catalog=livemymoments;Integrated Security=True");
SqlCommand cmd;
SqlDataReader dr;

protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("insert into marriage_config values(''" + DropDownList1.SelectedItem + "'',''" + DropDownList2.SelectedItem + "'',''" + DropDownList3.SelectedItem + "'',''" + DropDownList4.SelectedItem + "'',''" + DropDownList5.SelectedItem + "'',''" + DropDownList6.SelectedItem + "'')", con);
cmd.ExecuteNonQuery();

}
}


这篇关于将varchar值'System.Web.UI.WebControls.DropDownList'转换为数据类型int时,转换失败.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆