Click here to Skip to main content
15,913,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

XML
<%@ 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

HTML
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();

    }
}
Posted
Comments
Richard C Bishop 5-Nov-12 10:28am    
It appears as if you are trying to insert the entire DropDownList into marriage_config instead of a selected value. Is that your intent or are you looking for specific values?
Raghavanand 5-Nov-12 10:42am    
sorry for the trouble... solved this myself...
Richard C Bishop 5-Nov-12 10:55am    
Good job.

You need to do "DropDownList3.SelectedValue" or "DropDownList3.SelectedIndex" to get the selected values.
 
Share this answer
 
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();

}
}
HTML

 
Share this answer
 
Comments
bbirajdar 5-Nov-12 10:52am    
It should be selecteditem.value

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900