4 Replies - 10173 Views - Last Post: 21 July 2013 - 12:49 AM Rate Topic: -----

#1 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Gridview Edite/Delete/Insert/Update Command Not Working!

Posted 28 June 2011 - 01:50 PM

Hello!

In my gridview, the Delete, Edit, Insert Commands aren't working. The only thing that does work is when I click on Edit I get the textbox templates, but when I click on Update for the changes to be made I get an error. If I try to Delete a row I get an error as well, the Error spells out the event is being fired but isn't being handled. If I click on edit & then I hit cancel, that portion works.

Here is my code:
<asp:GridView ID="GridView1" runat="server" 
              AutoGenerateColumns="False" 
              DataKeyNames="ID"
              DataSourceID="SqlDataSource1" 
              onrowdeleted="GridView1_RowDeleted" 
              OnRowUpdated="GridView1_RowUpdated" 
              ShowFooter="true" 
              OnRowCommand="GridView1_RowCommand">
<Columns>
    <asp:CommandField ShowDeleteButton="True" 
                      ShowEditButton="True" />
    <asp:TemplateField HeaderText="ID" SortExpression="ID">
    <ItemTemplate>
    <asp:Label ID="lblID" runat="server" 
                          Text='<%#Eval("ID") %>'>
    </asp:Label>
    </ItemTemplate>
    <FooterTemplate>
    <asp:Button ID="btnInsert" runat="server" 
                Text="Insert" CommandName="Add" />
    </FooterTemplate>
    </asp:TemplateField>
    
    <asp:TemplateField HeaderText="FirstName" 
                       SortExpression="FirstName">
    <ItemTemplate>
    <asp:Label ID="lblFirstName" runat="server" 
               Text='<%#Eval("FirstName") %>'>
    </asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="txtFirstName" runat="server" 
                 Text='<%#Bind("FirstName") %>'>
    </asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>
    <asp:TextBox ID="txtFname" runat="server">
    </asp:TextBox>
    </FooterTemplate>
    </asp:TemplateField>
    
    <asp:TemplateField HeaderText="LastName" 
                       SortExpression="LastName">
    <ItemTemplate>
    <asp:Label ID="lblLastName" runat="server" 
               Text='<%#Eval("LastName") %>'>
    </asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="txtLastName" runat="server" 
                 Text='<%#Bind("LastName") %>'>
    </asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>
    <asp:TextBox ID="txtLname" runat="server">
    </asp:TextBox>
    </FooterTemplate>
    </asp:TemplateField>
    
    <asp:TemplateField HeaderText="Department" 
                       SortExpression="Department">
    <ItemTemplate>
    <asp:Label ID="lblDepartment" runat="server" 
               Text='<%#Eval("Department") %>'>
    </asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="txtDepartmentName" runat="server" 
                 Text='<%#Bind("Department") %>'>
    </asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>
    <asp:TextBox ID="txtDept" runat="server">
    </asp:TextBox>
    </FooterTemplate>
    </asp:TemplateField>
    
    <asp:TemplateField HeaderText="Location" 
                       SortExpression="Location">
    <ItemTemplate>
    <asp:Label ID="lblLocation" runat="server" 
               Text='<%#Eval("Location") %>'>
    </asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="txtLocation" runat="server" 
                 Text='<%#Bind("Location") %>'>
    </asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>
    <asp:TextBox ID="txtLoc" runat="server">
    </asp:TextBox>
    </FooterTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString2%>"
DeleteCommand="DELETE FROM [Employees] WHERE [ID] = @ID" 
InsertCommand="INSERT INTO [Employees] ([FirstName], 
[LastName],[Department], [Location]) 
VALUES (@FirstName, @LastName, @Department, @Location)"
SelectCommand="SELECT [ID], [FirstName], [LastName], 
[Department], [Location] FROM [Employees]"
UpdateCommand="UPDATE [Employees] SET 
[FirstName] = @FirstName, [LastName] = @LastName, 
[Department] = @Department, [Location] = @Location 
WHERE [ID] = @ID" OnInserted="SqlDataSource1_Inserted">

<DeleteParameters>
    <asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
    <asp:Parameter Name="FirstName" Type="String" />
    <asp:Parameter Name="LastName" Type="String" />
    <asp:Parameter Name="Department" Type="String" />
    <asp:Parameter Name="Location" Type="String" />
    <asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
    <asp:Parameter Name="FirstName" Type="String" />
    <asp:Parameter Name="LastName" Type="String" />
    <asp:Parameter Name="Department" Type="String" />
    <asp:Parameter Name="Location" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<asp:Label ID="lblMessage" runat="server" 
           Font-Bold="True"></asp:Label><br />


My Code Behind:
    protected void Page_Load(object sender, EventArgs e)
    {
        lblMessage.Text = "";
    }
    protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        lblMessage.Text = "Record Updated";
    }
    protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        lblMessage.Text = "Record Deleted";
    }


Any ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: Gridview Edite/Delete/Insert/Update Command Not Working!

#2 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Re: Gridview Edite/Delete/Insert/Update Command Not Working!

Posted 29 June 2011 - 09:17 AM

Ok I fixed somethings around & I think I am close on getting the Delete part working but I get the error: Must Declare Scalar variable.

Here's my code:

      protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
           string SQLDBConnString = ConfigurationManager.ConnectionStrings["Percut"].ToString();
                SqlConnection SQLDBConn = new SqlConnection(SQLDBConnString);

               

               GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];

            

            SQLDBConn.Open();

            SqlCommand cmd = new SqlCommand("Delete from dbo.Info WHERE USERID=@USERID" + "", SQLDBConn);

         
            cmd.ExecuteNonQuery();
             

            SQLDBConn.Close();

            GridView1.DataBind();

            lblMessage.Text = "Record Deleted";

        }


I tried cmd.Parameters.AddWithValue (@UserId, Int32);
but that didn't work, it's looking for an object value, what am I missing?
Was This Post Helpful? 0
  • +
  • -

#3 Moogoo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 82
  • Joined: 06-January 10

Re: Gridview Edite/Delete/Insert/Update Command Not Working!

Posted 30 June 2011 - 12:08 PM

What does it say when you click on Update, and if you could copy and paste the exact error, that would be helpful.
Was This Post Helpful? 0
  • +
  • -

#4 Moogoo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 82
  • Joined: 06-January 10

Re: Gridview Edite/Delete/Insert/Update Command Not Working!

Posted 30 June 2011 - 12:15 PM

I can also tell you that in your second posting, when it says you need to declare a scalar variable, it wants you declare a parameter for your SQL code and also will want you to assign a value to it. Right now you have @UserID for a parameter in your SQL, but there is nothing on the interface side saying what that is. After you declare your SQLCommand cmd, you need to add a parameter for UserID, like this:

SQLParameter prmUserID = new SQLParameter();
prmUserID.ParameterName = "@UserID";
prmUserID.Value = (wherever your userID is, so if it's in your GridView, it will be e.item.Cells[column number here].text);

Make sure you also add the parameter to your cmd.
Was This Post Helpful? 0
  • +
  • -

#5 Ushari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-July 13

Re: Gridview Edite/Delete/Insert/Update Command Not Working!

Posted 21 July 2013 - 12:49 AM

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<%-- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="EMPID" DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display." Width="723px">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="EMPID" HeaderText="EMPID" ReadOnly="True"
SortExpression="EMPID" />
<asp:BoundField DataField="ENAME" HeaderText="ENAME" SortExpression="ENAME" />
<asp:BoundField DataField="DESIGNATION" HeaderText="DESIGNATION"
SortExpression="DESIGNATION" />
<asp:BoundField DataField="DOJ" HeaderText="DOJ" SortExpression="DOJ" />
<asp:BoundField DataField="SALARY" HeaderText="SALARY"
SortExpression="SALARY" />
<asp:BoundField DataField="DEPTNO" HeaderText="DEPTNO"
SortExpression="DEPTNO" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:EmployeeConnectionString1 %>"
DeleteCommand="DELETE FROM [EMPDETAILS] WHERE [EMPID] = @EMPID"
InsertCommand="INSERT INTO [EMPDETAILS] ([EMPID], [ENAME], [DESIGNATION], [DOJ], [SALARY], [DEPTNO]) VALUES (@EMPID, @ENAME, @DESIGNATION, @DOJ, @SALARY, @DEPTNO)"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT [EMPID], [ENAME], [DESIGNATION], [DOJ], [SALARY], [DEPTNO] FROM [EMPDETAILS]"

UpdateCommand="UPDATE [EMPDETAILS] SET [ENAME] = @ENAME, [DESIGNATION] = @DESIGNATION, [DOJ] = @DOJ, [SALARY] = @SALARY, [DEPTNO] = @DEPTNO WHERE [EMPID] = @EMPID">
<DeleteParameters>
<asp:Parameter Name="EMPID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="EMPID" Type="Int32" />
<asp:Parameter Name="ENAME" Type="String" />
<asp:Parameter Name="DESIGNATION" Type="String" />
<asp:Parameter Name="DOJ" Type="DateTime" />
<asp:Parameter Name="SALARY" Type="Decimal" />
<asp:Parameter Name="DEPTNO" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="ENAME" Type="String" />
<asp:Parameter Name="DESIGNATION" Type="String" />
<asp:Parameter Name="DOJ" Type="DateTime" />
<asp:Parameter Name="SALARY" Type="Decimal" />
<asp:Parameter Name="DEPTNO" Type="Int32" />
<asp:Parameter Name="EMPID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
--%>
<br />
<br />
<br />
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="EMPNO" DataSourceID="SqlDataSource2" EmptyDataText="no records are available">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="EMPNO" HeaderText="EmpId" />
<asp:BoundField DataField="ENAME" HeaderText="EName" />
<asp:BoundField DataField="JOB" HeaderText="Job" />
<asp:BoundField DataField="MGR" HeaderText="Manager" />
<asp:BoundField DataField="HIREDATE" HeaderText="HireDate" />
<asp:BoundField DataField="SAL" HeaderText="Salary" />
<asp:BoundField DataField="COMM" HeaderText="Comm" />
<asp:BoundField DataField="DEPTNO" HeaderText="Deptno" />
</Columns>
</asp:GridView>
<br />




<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:Employee_DBConnectionString3 %>"
DeleteCommand="DELETE FROM EMP WHERE EMPNO=@Empno"
InsertCommand="INSERT INTO EMP VALUES(@Empno,@Ename,@Job,@Mgr,@Hiredate,@Sal,@Comm,@Deptno)"
SelectCommand="select EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO from EMP" UpdateCommand="UPDATE EMP SET ENAME=@Ename,JOB=@Job,MGR=@Mgr,HIREDATE=@Hiredate,SAl=@Sal,COMM=@Comm,DEPTNO=@Deptno
where EMPNO=@Empno">
<DeleteParameters>
<asp:Parameter Name="Empno" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Empno" />
<asp:Parameter Name="Ename" />
<asp:Parameter Name="Job" />
<asp:Parameter Name="Mgr" />
<asp:Parameter Name="Hiredate" />
<asp:Parameter Name="Sal" />
<asp:Parameter Name="Comm" />
<asp:Parameter Name="Deptno" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Ename" />
<asp:Parameter Name="Job" />
<asp:Parameter Name="Mgr" />
<asp:Parameter Name="Hiredate" />
<asp:Parameter Name="Sal" />
<asp:Parameter Name="Comm" />
<asp:Parameter Name="Deptno" />
<asp:Parameter Name="Empno" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
<br />
<br />


<br />
<br />
<br />
<br />
<br />

</div>
</form>
</body>
</html>




this code is working fine for me and no need to write any code in .cs file
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1