Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i have a gridview.it contains two empty text boxes,one dropdownlist control.for dropdownlist control data is taken from sql server.when i click on submit tha data in gridview(two textboxes,dropdownlis values)should be saved in sql server.my probelm when i click on submit,if same values selected in dropdownlist then an popup error must be displayed.
give the answer in asp using c#.



please help me.thanks in advance.

ASP.NET
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
    OnRowDataBound="GridView1_RowDataBound" >

    <Columns>
        <asp:TemplateField HeaderText="name">
            <ItemTemplate>
                  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="age">
            <ItemTemplate>
                  <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="sex">
            <ItemTemplate>
                <asp:DropDownList ID="ddlSex" runat="server">
                    <asp:ListItem> MALE </asp:ListItem>
                     <asp:ListItem> FEMALE </asp:ListItem>
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="seatno">
            <ItemTemplate>
                <asp:DropDownList ID="ddlSeatno" runat="server"/>
            </ItemTemplate>
        </asp:TemplateField>

    </Columns>
</asp:GridView>



C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView();
        }
        
    }

    private void BindGridView()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select seatno from busseats where busno='"+lblBusno.Text+"' and status='"+lblStatus.Text+"'", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            con.Open();
            var ddl = (DropDownList)e.Row.FindControl("ddlSeatno");
            //int CountryId = Convert.ToInt32(e.Row.Cells[0].Text);
            SqlCommand cmd = new SqlCommand("select seatno from busseats where busno='" + lblBusno.Text + "' and status='" + lblStatus.Text + "'", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            ddl.DataSource = ds;
            ddl.DataTextField = "seatno";
            //ddl.DataValueField = "StateID";
            ddl.DataBind();
            ddl.Items.Insert(0, new ListItem("--Select--", "0"));

        }
    }
Posted
Comments
vivektiwari97701 30-Oct-12 2:13am    
if r u using dynamic grid view then first of all save data in data table using view state.dont validate ddl value in submit button for validate in DDl event....
guvvadi 30-Oct-12 2:30am    
can you please tell me how
Sergey Alexandrovich Kryukov 30-Oct-12 2:20am    
Why doing such things? Bad, really bad idea.
--SA

Popup error? You certainly want to scare off your users. Do yourself a favor: no pop-ups. Better review your UI. If some value should not be selected by the user, it should not appear in the drop-down list. It will take some effort though, but the reputation of a developer who does not show useless trash on screen worth it. :-)

—SA
 
Share this answer
 
v2
its better to remove previously selected value in next DDl binding u can google it and check the below link for validation....

http://forums.asp.net/t/1705719.aspx/1[^]

http://forums.asp.net/t/1771811.aspx/1[^]
 
Share this answer
 

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