validation in datagrid while insert update in asp.net

Posted by abhi on Stack Overflow See other posts from Stack Overflow or by abhi
Published on 2010-05-14T09:08:27Z Indexed on 2010/05/14 9:14 UTC
Read the original article Hit count: 730

Filed under:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %>

<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>

Untitled Page  

                 <telerik:GridTemplateColumn Visible="false">
                    <ItemTemplate>
                        <asp:Label ID="lblEmpID" runat="server" Text='<%# bind("pid") %>'>
                        </asp:Label>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
               <telerik:GridBoundColumn UniqueName="Fname" HeaderText="Fname" DataField="Fname" CurrentFilterFunction="NotIsNull" SortExpression="Fname">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="Lname" HeaderText="Lname" DataField="Lname" CurrentFilterFunction="NotIsNull" SortExpression="Lname">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="Designation" HeaderText="Designation" DataField="Designation" CurrentFilterFunction="NotIsNull" SortExpression="Designation">
                </telerik:GridBoundColumn>


                   <telerik:GridEditCommandColumn>
                </telerik:GridEditCommandColumn>


                <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="column">
                </telerik:GridButtonColumn>

                </Columns>

            <EditFormSettings>
            <FormTemplate>
            <table>
            <tr>
            <td>Fname*</td>
            <td>
            <asp:HiddenField ID="Fname" runat="server" Visible="false" />
            <asp:TextBox ID="txtFname" runat="server" Text='<%#("Fname")%>'>
            </asp:TextBox>
            <asp:RequiredFieldValidator ID="EvalFname" ControlToValidate="txtFname"  ErrorMessage="Enter Name" runat="server" ValidationGroup="Update">
            *</asp:RequiredFieldValidator>

            </td>
            </tr>

            <tr>
            <td>Lname*</td>
            <td>
            <asp:HiddenField ID="HiddenField1" runat="server" Visible="false" />
            <asp:TextBox ID="txtLname" runat="server" Text='<%#("Lname")%>'>
            </asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtLname"  ErrorMessage="Enter Name" runat="server" ValidationGroup="Update">
            *</asp:RequiredFieldValidator>

            </td>
            </tr>
            <tr>
            <td>Designation*
            </td>
            <td>
            <asp:HiddenField ID="HiddenField2" runat="server" Visible="false" />
            <asp:TextBox ID="txtDesignation" runat="server" Text='<%#("Designation")%>'>
            </asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="txtDesignation"  ErrorMessage="Enter Designation" runat="server" ValidationGroup="Update">
            *</asp:RequiredFieldValidator>

            </td>
            </tr>

            </table>
            </FormTemplate>
            <EditColumn UpdateText="Update record" UniqueName="EditCommandColumn1" CancelText="Cancel edit">   
            </EditColumn>
            </EditFormSettings>
            </MasterTableView>
            </telerik:RadGrid>


</form>

this is my code i want to perform validation using the required validators but i think m missing smthin so pls help, here's my code behind using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using Telerik.Web.UI;

public partial class Default7 : System.Web.UI.Page { string strQry; public SqlDataAdapter da;

public DataSet ds;
public SqlCommand cmd;
public DataTable dt;
string strCon = "Data Source=MINETDEVDATA; Initial Catalog=ML_SuppliersProd; User Id=sa; Password=@MinetApps7;";

 public SqlConnection con;
protected void Page_Load(object sender, EventArgs e)
{



}

protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    dt = new DataTable();
    con = new SqlConnection(strCon);
    da = new SqlDataAdapter();
    try
    {


    strQry = "SELECT * FROM table2";
    con.Open();

    da.SelectCommand = new SqlCommand(strQry,con);
    da.Fill(dt);
    RadGrid1.DataSource = dt;
    }
    finally
    {
        con.Close();
    }
}
protected void RadGrid1_DeleteCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
    con = new SqlConnection(strCon);
    cmd = new SqlCommand();
    GridDataItem item = (GridDataItem)e.Item;

    string pid = item.OwnerTableView.DataKeyValues[item.ItemIndex]["pid"].ToString();



    con.Open();
    string delete = "DELETE from table2 where pid='"+pid+"'";
    cmd.CommandText = delete;
    cmd.Connection = con;
    cmd.ExecuteNonQuery();

    con.Close();
}
protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
{
    GridEditableItem radgriditem = e.Item as GridEditableItem;
    string pid = radgriditem.OwnerTableView.DataKeyValues[radgriditem.ItemIndex]["pid"].ToString();

    string firstname = (radgriditem["Fname"].Controls[0] as TextBox).Text;
    string lastname = (radgriditem["Lname"].Controls[0] as TextBox).Text;
    string designation = (radgriditem["Designation"].Controls[0] as TextBox).Text;
    con = new SqlConnection(strCon);
    cmd = new SqlCommand();
    try
    {

        con.Open();

        string update = "UPDATE table2 set Fname='" + firstname + "',Lname='" + lastname + "',Designation='" + designation + "' WHERE pid='" + pid + "'";
        cmd.CommandText = update;
        cmd.Connection = con;
        cmd.ExecuteNonQuery();
        con.Close();
    }
    catch (Exception ex)
    {
        RadGrid1.Controls.Add(new LiteralControl("Unable to update Reason: " + ex.Message));
        e.Canceled = true;   


    }

}
protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
{
    GridEditFormInsertItem insertitem = (GridEditFormInsertItem)e.Item;


    string firstname = (insertitem["Fname"].Controls[0] as TextBox).Text;
    string lastname = (insertitem["Lname"].Controls[0] as TextBox).Text;
    string designation = (insertitem["Designation"].Controls[0] as TextBox).Text;
    con = new SqlConnection(strCon);
    cmd = new SqlCommand();
    try
    {
        con.Open();
        String insertQuery = "INSERT into table1(Fname,Lname,Designation) Values ('" + firstname + "','" + lastname + "','" + designation + "')";
        cmd.CommandText = insertQuery;
        cmd.Connection = con;
        cmd.ExecuteNonQuery();
        con.Close();

    }
    catch(Exception ex)
    {
        RadGrid1.Controls.Add(new LiteralControl("Unable to insert Reason:" + ex.Message));
        e.Canceled = true;

    }

}

}

© Stack Overflow or respective owner

Related posts about ASP.NET