How to add country state city using different pages to database in ASP.Net

darsh
 
on Feb 15, 2013 11:08 PM
3000 Views

I have one page Country.aspx through that i add country in my database...

I have another page name State.aspx through which i can add states in my database..

Now The country i have added in database through first page when i want to add states in
database i want that added country to be shown in drop down list in my state.aspx page..

here in state.aspx page i have one drop down for country which i select and one textbox for
state which i want to add...how can i add states with this....here when i click on save the state
should be saved in database

how can i do that??

I have taken three different tables name country,state and city for that and 3 different pages
country.aspx,state.aspx,city.aspx ..

/*country.aspx*/


<form id="form1" runat="server">
    <div>
    <h2>Add Country</h2>
        <table>
            
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Country:"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnClear" runat="server" Text="Clear" 
                        onclick="btnClear_Click" />
                </td>
                <td>
                    <asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />
                </td>
            </tr>
        </table>
    </div>
    </form>

/* country.aspx.cs*/

 protected void btnSave_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into Country(Country_Name) values('" + txtCountry.Text + "')", con);
        int i = cmd.ExecuteNonQuery();
        Response.Write(i.ToString() + "Row(s) updated Successfully");
        
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Country.aspx");
    }

/*  state.aspx  */

<form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    <asp:Label ID="lblCountry" runat="server" Text="Label">Country:</asp:Label>
                </td>
                <td>
                    <asp:DropDownList ID="ddlCountry" runat="server" Width="200px" DataTextField="Country_Name" DataValueField="Country_Id">
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblState" runat="server">State:</asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtState" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnClear" runat="server" Text="Clear" 
                        onclick="btnClear_Click" />
                </td>
                <td>
                    <asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />
                </td>
            </tr>
        </table>
    </div>
    </form>

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Mustafa
 
on Feb 18, 2013 06:05 AM

Ok darsh you will have to first change your database table structures like this

CREATE TABLE Country
(
 CountryID INT IDENTITY,
 CountryName VARCHAR(100)
)

CREATE TABLE State
(
 StateID INT IDENTITY,
 CountryID INT,
 StateName VARCHAR(100)
)

Now the CountryID Column in Country Table and StateID Column in State Table are auto generated.

Now you will have to write the following codes on Country.aspx Page

//--------------------------Country.aspx Page-----------------------------

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;

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

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString);
            con.Open();
            SqlCommand cmd = new SqlCommand("INSERT INTO Country(CountryName) VALUES(@CN)", con);
            cmd.Parameters.Add(new SqlParameter("@CN", txtCountry.Text));
            int i = cmd.ExecuteNonQuery();
            Response.Write(i.ToString() + "Row(s) updated Successfully");

        }
        protected void btnClear_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Country.aspx");
        }
    }
}

Now you will have to write the following codes on State.aspx Page

//----------------------------------State.aspx Page--------------------

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;

namespace SampleProject
{
    public partial class State : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                DataTable dataTable = new DataTable();
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString);
                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT CountryID, CountryName FROM Country", con);
                SqlDataReader sqlDataReader = cmd.ExecuteReader();
                dataTable.Load(sqlDataReader);
                ddlCountry.DataSource = dataTable;
                ddlCountry.DataTextField = "CountryName";
                ddlCountry.DataValueField = "CountryID";
                ddlCountry.DataBind();
            }
            else
            {
                ddlCountry.SelectedItem.Value = ddlCountry.SelectedItem.Value;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString);
            con.Open();
            SqlCommand cmd = new SqlCommand("INSERT INTO State(CountryID,StateName) VALUES(@CI,@SN)", con);
            cmd.Parameters.Add(new SqlParameter("@CI", Convert.ToInt32(ddlCountry.SelectedItem.Value)));
            cmd.Parameters.Add(new SqlParameter("@SN", txtState.Text));
            int i = cmd.ExecuteNonQuery();
            Response.Write(i.ToString() + "Row(s) updated Successfully");
        }
    }
}

and check whether it works.