Search code examples
asp.netvalidationgridviewupdatecommand

unable to update in gridview when require validation occur in textbox


I want to modify items in gridview by update command but in same page i have also a textbox with required validation.

i'm unable to update in gridview when require validation occur in textbox Control enter image description here

And Source Code here...

 <div class="row">
    <asp:Button ID="Button1" runat="server" CssClass="btn btn-primary" Text="Add" OnClick="Button1_Click"
        ValidationGroup="btn" />
    <asp:Button ID="Button2" runat="server" CssClass="btn btn-primary" Text="Reset" OnClick="Button2_Click"
        CausesValidation="False" />
    <asp:Button ID="Button3" runat="server" CssClass="btn btn-primary" Text="Show" CausesValidation="False"
        OnClick="Button3_Click" />
</div>
</form> </div> </div>
<asp:GridView ID="GridView1" runat="server" CssClass="table-hover table-responsive table-condensed table-bordered"
    AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id"
    DataSourceID="SqlDataSource1" Visible="False">
    <Columns>
        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
        <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
            SortExpression="id" />
        <asp:BoundField DataField="Loc" HeaderText="Location" SortExpression="Loc" />
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:mycon %>"
    SelectCommand="SELECT * FROM [location]" DeleteCommand="Delete From Location Where Id=@id"
    UpdateCommand="update  location set loc=@loc where id=@id">
    <DeleteParameters>
        <asp:ControlParameter ControlID="TextBox1" Name="id" PropertyName="Text" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Name="loc" />
        <asp:ControlParameter ControlID="GridView1" Name="id" PropertyName="SelectedValue" />
    </UpdateParameters>
</asp:SqlDataSource>

and cs code here

protected void Button1_Click(object sender, EventArgs e)
{
    string qry = "insert into location (loc)values(@loc) ";
    SqlConnection con = Connection.Getconnection();
    con.Open();
    SqlCommand cmd = new SqlCommand(qry, con);
    cmd.Parameters.AddWithValue("@loc", TextBox1.Text);
    int x = cmd.ExecuteNonQuery();
    if (x > 0)
    {
        ClientScript.RegisterStartupScript(Page.GetType(), "validation!", "<script>alert('Successfully Add')</script>");
    }
    else
    {
        ClientScript.RegisterStartupScript(Page.GetType(), "validation!", "<script>alert('Error')</script>");
    }
}
protected void Button2_Click(object sender, EventArgs e)
{
    TextBox1.Text = string.Empty;
}
protected void Button3_Click(object sender, EventArgs e)
{
    GridView1.Visible = true;
}

Solution

    1. Please try to add try catch for button1_click to catch Exceptions.
    2. Validation group has an effect only when the value of the CausesValidation property is set to true.and validation group need to specify a value Please Refer this Link