Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <title>Register:</title>
    <asp:Label ID="lblFb" runat="server" />
    <center>
        <table cellpadding="10px">
            <tr>
                <td>
                    Name:
                </td>
                <td>
                    <asp:TextBox ID="txtName" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    Mobile:
                </td>
                <td>
                    <asp:TextBox ID="txtMobile" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    Age:
                </td>
                <td>
                    <asp:TextBox ID="txtAge" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    DoB:
                </td>
                <td>
                    <asp:TextBox ID="txtDoB" runat="server" />
                </td>
            </tr>
            <tr>
            <td>Country</td>
            <td><asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true"
                    onselectedindexchanged="ddlCountry_SelectedIndexChanged">
            </asp:DropDownList></td>
            </tr>
            <tr>
            <td>State</td>
            <td><asp:DropDownList ID="ddlState" runat="server" AutoPostBack="true"
                    onselectedindexchanged="ddlState_SelectedIndexChanged">
            <asp:ListItem Text="--select state--"></asp:ListItem>
            </asp:DropDownList></td>
            </tr>
            <tr>
            <td>City</td>
            <td><asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="true">
            <asp:ListItem Text="--select city--"></asp:ListItem>
            </asp:DropDownList></td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Button ID="btnSubmit" Text="Submit" runat="server" Font-Bold="true" OnClick="btnSubmit_Click" />
                </td>
            </tr>
        </table>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" Width="100%" DataKeyNames="ID"
            BackColor="#DEBA84" AutoGenerateColumns="false" BorderColor="#DEBA84" BorderStyle="None"
            BorderWidth="1px" CellPadding="5" CellSpacing="2"
            onrowediting="GridView1_RowEditing"
            onrowcancelingedit="GridView1_RowCancelingEdit"
            onrowupdating="GridView1_RowUpdating"
            onrowdatabound="GridView1_RowDataBound">
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <Columns>
                <asp:TemplateField HeaderText="Sr. No">
                    <ItemTemplate>
                        <%#Container.DisplayIndex+1 %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEditName" runat="server" Text='<%#Eval("Name")%>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblItemName" runat="server" Text='<%#Eval("Name") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Mobile">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEditMobile" runat="server" Text='<%#Eval("Mobile") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblItemMobile" runat="server" Text='<%#Eval("Mobile")%>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Age">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEditAge" runat="server" Text='<%#Eval("Age") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblItemAge" runat="server" Text='<%#Eval("Age") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Date of Birth">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEditDoB" runat="server" Text='<%#Eval("DoB") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblItemDoB" runat="server" Text='<%#Eval("DoB") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Country">
                <EditItemTemplate>
                <asp:DropDownList ID="ddleditCountry" runat="server" OnSelectedIndexChanged="ddlEditCountry_selectedIndexChange" AutoPostBack="true" />
                 <asp:HiddenField ID="hdnCountry" runat="server" Value='<%#Eval("Country") %>' />
                </EditItemTemplate>
                <ItemTemplate>
                <center>
                <asp:Label ID="lblCountry" runat="server" Text='<%#Eval("Country") %>' />
                </center>
                </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="State">
                <EditItemTemplate>
                <asp:DropDownList ID="ddleditState" runat="server" />
                </EditItemTemplate>
                <ItemTemplate>
                <center>
                <asp:Label ID="lblState" runat="server" Text='<%#Eval("State") %>' />
                </center>
                </ItemTemplate>
                </asp:TemplateField>
                 <asp:TemplateField HeaderText="City">
                <EditItemTemplate>
                <asp:DropDownList ID="ddleditCity" runat="server" />
                </EditItemTemplate>
                <ItemTemplate>
                <center>
                <asp:Label ID="lblCity" runat="server" Text='<%#Eval("City") %>' />
                </center>
                </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Edit">
                    <EditItemTemplate>
                        <asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/Images/update.jpg"
                            ToolTip="Update" Height="20px" Width="20px" />
                        <asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/Cancel.jpg"
                            ToolTip="Cancel" Height="20px" Width="20px" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:ImageButton ID="imgbtnEdit" runat="server" ImageUrl="~/Images/Edit.jpg" ToolTip="Edit"
                            Height="20px" Width="20px" CommandName="Edit" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Delete">
                    <ItemTemplate>
                        <asp:ImageButton ID="imgbtnDelete" runat="server" ImageUrl="~/Images/delete.jpg"
                            ToolTip="Delete" Height="20px" Width="20px" OnClick="GridView1_RowDeleting" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BorderColor="#FF99FF" />
        </asp:GridView>
    </center>
</asp:Content>
 

CS Code:
 
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.Web.UI.HtmlControls;
 

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(@"server=DELL\SQLEXPRESS; Database=Bussiness; user id=sa; Password=vaishnavisingh;");
    SqlParameter[] prm;
    SqlDataAdapter da = new SqlDataAdapter();
    SqlCommand cmd = new SqlCommand();
    DataSet ds = new DataSet();
    string str = "";
    static int id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            bindGrid();
            bindcountry();
            
        }
 
    }
    protected void bindGrid()
    {
        con.Open();
        cmd = new SqlCommand("Select * From Grid", con);
        da = new SqlDataAdapter(cmd);
        ds = new DataSet();
        da.Fill(ds);
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        prm = new SqlParameter[7];
        prm[0] = new SqlParameter("@Name", txtName.Text.Trim());
        prm[1] = new SqlParameter("@Mobile", txtMobile.Text.Trim());
        prm[2] = new SqlParameter("@Age", txtAge.Text.Trim());
        prm[3] = new SqlParameter("@DoB", txtDoB.Text.Trim());
        prm[4] = new SqlParameter("@Country", ddlCountry.SelectedItem.Text);
        prm[5] = new SqlParameter("@State", ddlState.SelectedItem.Text);
        prm[6] = new SqlParameter("@City", ddlCity.SelectedItem.Text);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "insertData";
        cmd.Connection = con;
        cmd.Parameters.AddRange(prm);
        con.Open();
        int result = cmd.ExecuteNonQuery();
        con.Close();
        if (result > 0)
        {
            Response.Write("Registered successfully");
            Response.Redirect("Default.aspx");
        }
 
    }
 

    protected void GridView1_RowDeleting(object sender, EventArgs e)
    {
        ImageButton lnk = ((ImageButton)(GridView1.FindControl("imgbtnDelete")));
        lnk = (ImageButton)sender;
        GridViewRow gvr = (GridViewRow)lnk.NamingContainer;
        String str = GridView1.DataKeys[gvr.RowIndex].Value.ToString();
        int id = Convert.ToInt32(str);
        prm = new SqlParameter[1];
        prm[0] = new SqlParameter("@id", id);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "deleteGrid";
        cmd.Connection = con;
        con.Open();
        cmd.Parameters.AddRange(prm);
        int result = cmd.ExecuteNonQuery();
        if (result > 0)
        {
            Response.Redirect("Default.aspx");
        }
 

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bindGrid();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bindGrid();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow gvr = GridView1.Rows[e.RowIndex];
        String str = GridView1.DataKeys[gvr.RowIndex].Value.ToString();
        int id = Convert.ToInt32(str);
        String editName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditName")).Text;
        String editMobile = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditMobile")).Text;
        String editAge = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditAge")).Text;
        prm = new SqlParameter[2];
        prm[0] = new SqlParameter("@id", id);
        prm[1] = new SqlParameter("@RoomType", editName);
 
        Response.Redirect("addRoomType.aspx");
        //Its Ryt Code//
    }
    protected void bindcountry()
    {
        ddlCountry.AppendDataBoundItems = true;
        ddlCountry.Items.Add(new ListItem("--Select Country--", ""));
        String strQry = "select Countryid, countryname from country";
        cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = strQry;
        cmd.Connection = con;
        try
        {
            con.Open();
            ddlCountry.DataSource = cmd.ExecuteReader();
            ddlCountry.DataTextField = "CountryName";
            ddlCountry.DataValueField = "CountryID";
            ddlCountry.DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            con.Dispose();
        }
    }
    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlState.Items.Clear();
        ddlState.Items.Add(new ListItem("--select state--", ""));
        ddlCity.Items.Clear();
        ddlCity.Items.Add(new ListItem("--select city--", ""));
        ddlState.AppendDataBoundItems = true;
        String Qry = "Select StateID, StateName from State where CountryID=@CountryID";
        cmd = new SqlCommand();
        cmd.Parameters.AddWithValue("@CountryID", ddlCountry.SelectedItem.Value);
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = Qry;
        cmd.Connection = con;
        try
        {
            con.Open();
            ddlState.DataSource = cmd.ExecuteReader();
            ddlState.DataTextField = "StateName";
            ddlState.DataValueField = "StateID";
            ddlState.DataBind();
            if (ddlState.Items.Count > 1)
            {
                ddlState.Enabled = true;
            }
            else
            {
                ddlState.Enabled = false;
                ddlCity.Enabled = false;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            con.Dispose();
        }
    }
    protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlCity.Items.Clear();
        ddlCity.Items.Add(new ListItem("--select city--", ""));
        ddlCity.AppendDataBoundItems = true;
        String Qry = "select Cityid, cityname from city where Stateid=@StateID";
        cmd = new SqlCommand();
        cmd.Parameters.AddWithValue("@StateID", ddlState.SelectedItem.Value);
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = Qry;
        cmd.Connection = con;
        try
        {
            con.Open();
            ddlCity.DataSource = cmd.ExecuteReader();
            ddlCity.DataTextField = "CityName";
            ddlCity.DataValueField = "CityID";
            ddlCity.DataBind();
            if (ddlCity.Items.Count > 1)
            {
                ddlCity.Enabled = true;
            }
            else
            {
                ddlCity.Enabled = false;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            con.Dispose();
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (GridView1.EditIndex == e.Row.RowIndex && e.Row.RowType == DataControlRowType.DataRow)
        {
            
            DropDownList ddllist1 = ((DropDownList)e.Row.FindControl("ddleditCountry"));
            var hdnCountryName = ((HiddenField)e.Row.FindControl("hdnCountry"));
            ddllist1.AppendDataBoundItems = true;
            
            String strQry = "select Countryid, countryname from country";
            cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = strQry;
            cmd.Connection = con;
            try
            {
                con.Open();
                ddllist1.DataSource = cmd.ExecuteReader();
                ddllist1.DataTextField = "CountryName";
                ddllist1.DataValueField = "CountryID";
                ddllist1.DataBind();
                ddllist1.Items.Insert(0,new ListItem("--Select Country--", "0"));
                ddllist1.Items.FindByText(hdnCountryName.Value).Selected=true;
               
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }
 
    }
 
    protected void ddlEditCountry_selectedIndexChange(object sender, EventArgs e)
    {
        var ddl1 = ((DropDownList)(GridView1.FindControl("ddleditCountry")));
        ddl1 = (DropDownList)sender;
        GridViewRow gvr = (GridViewRow)ddl1.NamingContainer;
        
 
            ddlState.Items.Clear();
            ddlState.Items.Add(new ListItem("--select state--", ""));
            ddlCity.Items.Clear();
            ddlCity.Items.Add(new ListItem("--select city--", ""));
            ddlState.AppendDataBoundItems = true;
            String Qry = "Select StateID, StateName from State where CountryID=@CountryID";
            cmd = new SqlCommand();
            cmd.Parameters.AddWithValue("@CountryID", ddlCountry.SelectedItem.Value);
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = Qry;
            cmd.Connection = con;
            try
            {
                con.Open();
                ddlState.DataSource = cmd.ExecuteReader();
                ddlState.DataTextField = "StateName";
                ddlState.DataValueField = "StateID";
                ddlState.DataBind();
                if (ddlState.Items.Count > 1)
                {
                    ddlState.Enabled = true;
                }
                else
                {
                    ddlState.Enabled = false;
                    ddlCity.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }
    
}
Posted 20-Jan-13 0:52am
Sujee1922
Edited 20-Jan-13 16:58pm
v3
Comments
Christian Graus at 20-Jan-13 20:37pm
   
If you are new to ASP.NET, then this is way too complex for you. ASP.NET is also nowhere near as good as MVC, so just move to MVC, obviously if you're new you're not on a real project, so learn the better framework
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
Sujee1 at 21-Jan-13 4:28am
   
thnx mam..bt i hv 3 dropdownlist in gridview.. country, state nd city..when i select the country then state will be changed automatically according to country....& so on....so pls help me to solve this
Vani Kulkarni at 21-Jan-13 4:43am
   
I have provided the links that can help you to start working your issue. You need to figure out or modify the code per your requirements.
Sujee1 at 22-Jan-13 3:20am
   
thnx alot mam...i hv solved my problem...i was putting the same name of the states in both..in the gridview nd outside the gridview...bt thnk u vry much for helping me to solve this.........:)
Vani Kulkarni at 22-Jan-13 4:29am
   
Great!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

just change the name of the dropdownlist control ddlstate...b'coz its alredy exist....
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 8-May-13 1:43am
   
Are you talking to yourself? Or just cheating? :-) —SA
thatraja at 12-Oct-13 2:37am
   
This person always close the his questions by posting his solutions(which is rare). He got 5! from me for his answers.
Sergey Alexandrovich Kryukov at 12-Oct-13 20:04pm
   
Why? I think this is nothing but abuse and cheating. If one has a problem and a solution which might be interesting to other people, this person should write some article, and let's see how people accept this publication... The gibberish we see here has no value, as any other gibberish... —SA
thatraja at 12-Oct-13 20:30pm
   
Not all OP returns again to post solutions to their answers. Atleast he did post the solution which could help the others in future. We can't expect all OPs to write article/Tip. BTW I didn't vote this one, only other answers of him.
Sergey Alexandrovich Kryukov at 12-Oct-13 20:51pm
   
I see. But then nothing should be posted. This is garbage. What's the value can you see in such question and self-answer? If everyone is going to show their bugs and then fixes, it will fill the site with trash... —SA
thatraja at 14-Oct-13 2:59am
   
We can't expect all OPs(most of them are newbie) to post article or tip/trick regarding this. They hardly come here to ask questions. Atleast this person has posted solution for his question which could help others in future. Besides he's not after reputation points IMO. Now this site has 1,248*20 questions without answers, if OP like this person starts to post solutions for their question possibly the count(unanswered questions) will decrease. Hope you got the point now.
Sergey Alexandrovich Kryukov at 14-Oct-13 3:25am
   
By the way, so many not answered questions is the problem, but why? Not because those questions were not answered, but, in absolute majority of cases, because they were not... removed for abuse. Yes, there are decent questions which were not answered because they were difficult (or just because the resolution would require too much time), or because they were on narrow field which was unknown to all experts. But such cases are relatively rare. Majority of non-answered questions were not answered because they are not formulated in sufficient detail, or because they are nonsense... Sad but true. —SA
Sujee1 at 14-Oct-13 3:06am
   
Mr. Sergey Alexandrovich Kryukov ,I am not thinking that i have done anything wrong or like trashing or garbage.. when i post a problem or ask something here, it does not mean that i am stop trying to solve it myself BTW keep trying. And when i get solution of my problem, i post my solution. And i post my solution because it might be helpful for others in future.
Sergey Alexandrovich Kryukov at 14-Oct-13 3:21am
   
I am not questioning your good intention. Your question is a real one, you paid a decent effort to solve the problem and to formulate it. But I cannot agree that your answer has any value except indicating that your problem does not exist anymore. I just cannot. You did not even properly explained the problem you resolved. And if this is really using the name which already exists, how can it help anyone? Of course you should not use the same name as already existing one, but this is something which will be told by a compiler to each person making this mistake, not really your post. Just think about it. —SA
Sujee1 at 14-Oct-13 3:58am
   
Okay but i am not specially talking about this one. It might be not well formed but it was a problem for me when i was very very new to asp.net.(Posted on 20-Jan-13 5:52am). but thank you so much for your good answering intention.
Sergey Alexandrovich Kryukov at 14-Oct-13 11:22am
   
Thank you for understanding. —SA

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 963
1 Tadit Dash 270
2 Sergey Alexandrovich Kryukov 250
3 OriginalGriff 233
4 Peter Leow 180
0 Sergey Alexandrovich Kryukov 9,435
1 OriginalGriff 5,498
2 Peter Leow 4,230
3 Maciej Los 3,540
4 Abhinav S 3,353


Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid