Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am working on the Grid CRUD operations and I want to use both Stored procedure and View separately.Here what I have tried

What I have tried:

C#
<pre>protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { bind();
            
            BindGrid();
           
            BindDepartment();
            }
        }

protected void bind()
        {
            cn.Open();
            SqlCommand cmd = new SqlCommand("Select * from Record", cn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            cn.Close();
            if (ds.Tables[0].Rows.Count > 0)
            {
                gvDetails.DataSource = ds;
                gvDetails.DataBind();
            }

        }
protected void gvDetails_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            DataTable dt = new DataTable();

            try
            {

                cn.Open();
                SqlCommand cmd = new SqlCommand("Select * from Record", cn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                cn.Close();
                gvDetails.DataSource = ds;


            }
            finally
            {


            }
        }

protected void INSERTEmployee(string Id, string name, string department, string age, string salary)
        {

            SqlCommand cmd = new SqlCommand();
            cn.Open();
            cmd.Connection = cn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "gvempdetails";

            cmd.Parameters.Add(new SqlParameter("@Action", SqlDbType.VarChar, 50));
            cmd.Parameters.Add(new SqlParameter("@EMPLOYEEID", int.Parse(age.Trim())));
            cmd.Parameters.Add(new SqlParameter("@EMPLOYEENAME", SqlDbType.VarChar, 50));
            cmd.Parameters.Add(new SqlParameter("@DEPARTMENT", SqlDbType.VarChar, 50));
            cmd.Parameters.Add(new SqlParameter("@AGE", SqlDbType.VarChar, 50));
            cmd.Parameters.Add(new SqlParameter("@SALARY", int.Parse(salary.Trim())));

            cmd.Parameters["@Action"].Value = "INSERT";
            cmd.Parameters["@EMPLOYEEID"].Value = Id;
            cmd.Parameters["@EMPLOYEENAME"].Value = name;
            cmd.Parameters["@DEPARTMENT"].Value = department;
            cmd.Parameters["@AGE"].Value = age;
            cmd.Parameters["@SALARY"].Value = salary;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            gvDetails.DataSource = dt;
            gvDetails.DataBind();
            cn.Close();


        }
        protected void gvDetails_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
           if (e.CommandName.Equals("Add"))
            {

            GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;
            string EmployeeId = (insertedItem["EmpId"].Controls[0] as TextBox).Text;
            string EmployeeName = (insertedItem["EmpName"].Controls[0] as TextBox).Text;
            string Department = (insertedItem["Dep"].Controls[0] as TextBox).Text;
            string Age = (insertedItem["Age"].Controls[0] as TextBox).Text;
            string Sal = (insertedItem["Sal"].Controls[0] as TextBox).Text;
            INSERTEmployee(EmployeeId, EmployeeName, Department, Age, Sal);
            bind();
}}
ASP.NET
<pre lang="HTML"><pre><div>
            <table style="margin-top:-25px;margin-bottom:100px;width:100%" >
                <tr>
                    <td>

            <telerik:RadGrid ID="gvDetails"  runat="server"  Skin="WebBlue"
            GridLines="None" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" 
             OnNeedDataSource="gvDetails_NeedDataSource" OnDeleteCommand="gvDetails_DeleteCommand" 
           OnInsertCommand="gvDetails_InsertCommand" OnUpdateCommand="gvDetails_UpdateCommand" EnableAJAX="True" PageSize="15"  > 
            <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle> 
              
            <MasterTableView DataKeyNames="EmpId" GridLines="None" Width="100%" CommandItemDisplay ="Top" > 
              
                <Columns> 
                    
                    <telerik:GridBoundColumn DataField="EmpId" ColumnEditorID="EmpId" HeaderText="EmployeeID" UniqueName="EmpId" >  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="EmpName" HeaderText="EmployeeName" UniqueName="EmpName">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Dep" HeaderText="Department" UniqueName="Dep">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Age" HeaderText="Age" UniqueName="Age">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Sal" HeaderText="Salary" UniqueName="Sal">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="Delete">  
                    </telerik:GridButtonColumn> 
                    <telerik:GridButtonColumn CommandName="Edit" Text="Edit" UniqueName="Edit"> 
                    </telerik:GridButtonColumn> 
                </Columns> 

                <EditFormSettings ColumnNumber="2" CaptionFormatString="Edit details for employee with ID {0}" 
                    CaptionDataField="EmpId">  
                    <FormTableItemStyle Wrap="False"></FormTableItemStyle> 
                    <FormCaptionStyle CssClass="EditFormHeader"></FormCaptionStyle> 
                    <FormMainTableStyle CellSpacing="0" CellPadding="3" Width="100%" /> 
                    <FormTableStyle GridLines="Horizontal" CellSpacing="0" CellPadding="2" CssClass="module" 
                        Height="110px" Width="100%" /> 
                    <FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle> 
                    <FormStyle Width="100%" BackColor="#EEF2EA"></FormStyle> 
                    <EditColumn UpdateText="Update record" UniqueName="EditCommandColumn1" CancelText="Cancel">  
                    </EditColumn> 
                    <FormTableButtonRowStyle HorizontalAlign="Right" CssClass="EditFormButtonRow"></FormTableButtonRowStyle> 
                </EditFormSettings> 
                <ExpandCollapseColumn Visible="False">  
                    <HeaderStyle Width="19px"></HeaderStyle> 
                </ExpandCollapseColumn> 
                <RowIndicatorColumn Visible="False">  
                    <HeaderStyle Width="20px" /> 
                </RowIndicatorColumn> 
            </MasterTableView> 
        </telerik:RadGrid>
                        </td>
                    </tr>
                </table> 
        </div>
SQL
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[gvempdetails]

     @Action VARCHAR (10),
     @EMPLOYEEID INT =0,
     @EMPLOYEENAME VARCHAR(100)='',
     @DEPARTMENT VARCHAR(100)= '',
     @AGE VARCHAR(30)= '',
     @SALARY INT = 0
     
AS
BEGIN
      SET NOCOUNT ON 
     IF @Action = 'SELECT'
      BEGIN
      SELECT EmpId,EmpName,Dep,Age,Sal
      FROM gvdetails17
       END
       
     IF @Action='INSERT'
       BEGIN
       INSERT INTO gvdetails17(EmpId,EmpName,Dep,Age,Sal) VALUES(@EMPLOYEEID,@EMPLOYEENAME,@DEPARTMENT,@AGE,@SALARY)
        END       
            
     IF @Action ='UPDATE'
      BEGIN
      UPDATE gvdetails17 SET EmpName=@EMPLOYEENAME,Dep=@DEPARTMENT,Age=@AGE,Sal=@SALARY WHERE EmpId=@EMPLOYEEID
      END
      
      
     IF @Action='DELETE'
      BEGIN
      DELETE FROM gvdetails17 where EmpId=@EMPLOYEEID
      END
      SET NOCOUNT OFF
      END
Posted
Updated 21-Mar-17 3:14am
v2
Comments
Karthik_Mahalingam 21-Mar-17 6:06am    
what is the issue
Member 12605293 21-Mar-17 6:09am    
I dont get any issue Karthik but the function is not performing,it is just postbacking thats it
Karthik_Mahalingam 21-Mar-17 6:11am    
which function? post back on which event ?
not clear.
Member 12605293 21-Mar-17 6:20am    
When I try to insert a record it is not inserting .I Dont know what is the problem
Karthik_Mahalingam 21-Mar-17 6:24am    
is the event gvDetails_InsertCommand firing?

1 solution

closing this post by posting as solution from comments,

Validate for null values of an object before accessing the members of it
validate for proper numeric format string before casting it an integer, you shall use Int32.TryParse Method [^] to convert to an int value, but you will have to figure out why the value turned out be null at the first place.
cmd.Parameters.Add(new SqlParameter("@EMPLOYEEID", int.Parse(id.Trim())));
 
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