显示国家,州,城市并保存在sql server数据库表中? [英] Display Country, State, City and save in sql server database table ?

查看:89
本文介绍了显示国家,州,城市并保存在sql server数据库表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



我开发了小型上传页面

页面包含:标题(下拉列表),描述(textBox),价格(textBox)数量(textBox),折扣(textBox),类型(下拉列表),国家(下拉列表),州(下拉列表),城市(下拉列表),图像(Fileuploadbutton)



这里填写所有文本框和下拉列表框和图像



一旦我选择国家(印度)自动将进入状态下拉列表框(选择Andra pradesh)并自动进入城市下拉列表框(选择海德拉巴)



填写这些保存在数据库表后但不保存国家,州,城市



保存在表格中:



(Id)2(标题)汽车(描述)dsdsds(价格)123.00(数量)1(折扣)100.00(类型)奥迪(国家)1(州)System.Web.UI.WebControls.DropDownList(市)System.Web.UI.WebControls.DropDownList



here country,state,cit y输出是:印度,Andra Pradesh,hyderabad



但显示输出为:1,System.Web.UI.WebControls.DropDownList,System.Web.UI.WebControls .DropDownList





这里有什么错误错误的地方请回复我



源代码:



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

推荐答案

In the button click event you need to modify your code as below...

In the button click event you need to modify your code as below...
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.


make sure that country,state and city dropdownlists should be select some item.


In the query, use ddl.selectedItem or ddl.SelectedValue instead of ddl.Text.
In the query, use ddl.selectedItem or ddl.SelectedValue instead of ddl.Text.


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:

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: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[+]


这篇关于显示国家,州,城市并保存在sql server数据库表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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