Click here to Skip to main content
15,908,901 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dear All,

I developed small upload page
the page contain: Title(Dropdownlist), Description(textBox), Price(textBox) Quantity(textBox),Discount(textBox), Type(Dropdownlist), Country(Dropdownlist), State(Dropdownlist), City(Dropdownlist), Image(Fileuploadbutton)

here once Fill all textboxes and dropdownlist boxes and image

and once i select country(India) automatically will come in state drop downlist box(Select Andra pradesh) and automatically will come in cities dropdown list box(select hyderabad)

after filling these save in database table but not save country, state, city

save in table like this:

(Id)2 (Title)Cars (Description)dsdsds (Price)123.00 (Quantity)1 (Discount)100.00 (Type)Audi (Country)1 (State)System.Web.UI.WebControls.DropDownList (City)System.Web.UI.WebControls.DropDownList

here country, state, city output is: India, Andra Pradesh, hyderabad

but display output is: 1, System.Web.UI.WebControls.DropDownList, System.Web.UI.WebControls.DropDownList


here what mistake where logic is missed please reply me

source code:

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 70%;
        }
        .style2
        {
            height: 23px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <table align="center" class="style1">
            <tr>
                <td style="font-size: x-large; font-style: italic;">
                    Upload</td>
                <td>
                     </td>
            </tr>
            <tr>
                <td>
                    Title :</td>
                <td>
                    <asp:DropDownList ID="DropDownList2" runat="server">
                        <asp:ListItem>Select
                        <asp:ListItem>Cars
                        <asp:ListItem>Jobs
                        <asp:ListItem>Flats Houses
                        
                    
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                        ControlToValidate="DropDownList2" ErrorMessage="select title">
                </td>
            </tr>
            <tr>
                <td>
                    Description :</td>
                <td>
                    <asp:TextBox ID="TextBox5" runat="server" TextMode="MultiLine">
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                        ControlToValidate="TextBox5" ErrorMessage="Enter Description">
                </td>
            </tr>
            <tr>
                <td>
                    Price :</td>
                <td>
                    <asp:TextBox ID="TextBox7" runat="server">
                </td>
            </tr>
            <tr>
                <td>
                    Quantity :</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server">
                </td>
            </tr>
            <tr>
                <td>
                    Discount :</td>
                <td>
                    <asp:TextBox ID="TextBox9" runat="server">
                </td>
            </tr>
            <tr>
                <td class="style2">
                     Type :</td>
                <td class="style2">
                    <asp:DropDownList ID="DropDownList1" runat="server">
                        <asp:ListItem>--Select--
                        <asp:ListItem>Audi
                        <asp:ListItem>Jeep
                        <asp:ListItem>Software Engineer
                        <asp:ListItem>Accountant
                        <asp:ListItem>Clerk
                        <asp:ListItem>2 BHK
                        <asp:ListItem>3 BHK
                    
                </td>
            </tr>
            <tr>
                <td>
                    Country :</td>
                <td>
                    <asp:DropDownList ID="ddlCountries" runat="server" AutoPostBack="True" 
                        OnSelectedIndexChanged="Country_Changed">
                    
                </td>
            </tr>
            <tr>
                <td>
                    State :</td>
                <td>
                    <asp:DropDownList ID="ddlStates" runat="server" AutoPostBack="true" 
                        OnSelectedIndexChanged="State_Changed">
                    
                </td>
            </tr>
            <tr>
                <td>
                    City :</td>
                <td>
                    <asp:DropDownList ID="ddlCities" runat="server" 
                        onselectedindexchanged="ddlCities_SelectedIndexChanged">
                    
                </td>
            </tr>
            <tr>
                <td>
                    ImagePath :</td>
                <td>
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                     </td>
                <td>
                     </td>
            </tr>
            <tr>
                <td>
                     </td>
                <td>
                    <asp:Button ID="Button1" runat="server" BackColor="#FF6600" 
                        BorderColor="#FF6600" Height="35px" onclick="Button1_Click" 
                        Text="Upload Images" />
                </td>
            </tr>
            <tr>
                <td>
                     </td>
                <td>
                     </td>
            </tr>
            <tr>
                <td>
                     </td>
                <td>
                    <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                     </td>
                <td>
                     </td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>


Code behind: Upload.aspx.cs:

<pre lang="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.SqlClient;
using System.Configuration;

public partial class Admin_Upload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string query = "select CountryId, CountryName from Country";
            BindDropDownList(ddlCountries, query, "CountryName", "CountryId", "Select Country");
            ddlStates.Enabled = false;
            ddlCities.Enabled = false;
            ddlStates.Items.Insert(0, new ListItem("Select State", "0"));
            ddlCities.Items.Insert(0, new ListItem("Select City", "0"));
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        string s = @"~\ProductImage\" + FileUpload1.FileName;
        FileUpload1.PostedFile.SaveAs(Server.MapPath(s));
        SqlConnection a = new SqlConnection(ConfigurationManager.ConnectionStrings["abcConnectionString"].ConnectionString);
        String cmd = "insert into Products(Title,Description,Price,Quantity,Discount,Type,Country,State,City,ImagePath) values('" + DropDownList2.Text + "','" + TextBox5.Text + "','" + TextBox7.Text + "','" + TextBox2.Text + "','" + TextBox9.Text + "','" + DropDownList1.Text + "','" + ddlCountries.Text + "','" + ddlStates + "','" + ddlCities + "','" + s + "')";

        SqlCommand b = new SqlCommand(cmd, a);

        a.Open();
        b.ExecuteNonQuery();
        a.Close();

    }
    private void BindDropDownList(DropDownList ddl, string query, string text, string value, string defaultText)
    {
        string conString = ConfigurationManager.ConnectionStrings["abcConnectionString"].ConnectionString;
        SqlCommand cmd = new SqlCommand(query);
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                con.Open();
                ddl.DataSource = cmd.ExecuteReader();
                ddl.DataTextField = text;
                ddl.DataValueField = value;
                ddl.DataBind();
                con.Close();
            }
        }
        ddl.Items.Insert(0, new ListItem(defaultText, "0"));
    }
    protected void Country_Changed(object sender, EventArgs e)
    {
        ddlStates.Enabled = false;
        ddlCities.Enabled = false;
        ddlStates.Items.Clear();
        ddlCities.Items.Clear();
        ddlStates.Items.Insert(0, new ListItem("Select State", "0"));
        ddlCities.Items.Insert(0, new ListItem("Select City", "0"));
        int countryId = int.Parse(ddlCountries.SelectedItem.Value);
        if (countryId > 0)
        {
            string query = string.Format("select StateId, StateName from State where CountryId = {0}", countryId);
            BindDropDownList(ddlStates, query, "StateName", "StateId", "Select State");
            ddlStates.Enabled = true;
        }
    }
    protected void State_Changed(object sender, EventArgs e)
    {
        ddlCities.Enabled = false;
        ddlCities.Items.Clear();
        ddlCities.Items.Insert(0, new ListItem("Select City", "0"));
        int stateId = int.Parse(ddlStates.SelectedItem.Value);
        if (stateId > 0)
        {
            string query = string.Format("select RegionId, RegionName from RegionTable where StateId = {0}", stateId);
            BindDropDownList(ddlCities, query, "RegionName", "RegionId", "Select Region");
            ddlCities.Enabled = true;
        }
    }
    protected void ddlCities_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

Above code what mistakes please reply me
Posted

In the button click event you need to modify your code as below...
C#
protected void Button1_Click(object sender, EventArgs e)
    {

        string s = @"~\ProductImage\" + FileUpload1.FileName;
        FileUpload1.PostedFile.SaveAs(Server.MapPath(s));
        SqlConnection a = new SqlConnection(ConfigurationManager.ConnectionStrings["abcConnectionString"].ConnectionString);
        String cmd = "insert into Products(Title,Description,Price,Quantity,Discount,Type,Country,State,City,ImagePath) values('" + DropDownList2.Text + "','" + TextBox5.Text + "','" + TextBox7.Text + "','" + TextBox2.Text + "','" + TextBox9.Text + "','" + DropDownList1.Text + "','" + ddlCountries..SelectedItem.Value + "','" + ddlStates.SelectedItem.Value + "','" + ddlCities.SelectedItem.Value + "','" + s + "')";

        SqlCommand b = new SqlCommand(cmd, a);

        a.Open();
        b.ExecuteNonQuery();
        a.Close();

    }

make sure that country,state and city dropdownlists should be select some item.
 
Share this answer
 
Comments
member1431 24-Feb-14 1:20am    
Thank you
Ramug10 24-Feb-14 1:34am    
WC
In the query, use ddl.selectedItem or ddl.SelectedValue instead of ddl.Text.
 
Share this answer
 
Comments
member1431 24-Feb-14 1:11am    
That is will get error

ddlstates.selectedItem.insert(...........)

here insert error occuring
I hope I am not missing anything but the code all seems in order at least. The only problem is in the ASP.Net code...

You have four values in the tags that you do not specify, here is an example of a ListItem:
ASP.NET
<asp:listitem>
Enabled="True|False"
Selected="True|False"
Text="The text that will be displayed"
Value="The actual value(ID) for the item" </asp:listitem>


Make sure you take a look at this[+]
 
Share this answer
 
v2

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