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:
<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