2013-04-08 271 views
4

我有一個可編輯的gridview,用戶可以通過按linkbutton編輯每一行。我收到一個錯誤,它停止了我的工作。請幫幫我。可編輯gridview不工作

HTML

<asp:GridView ID="gvList" runat="server" class="gv" CellPadding="2" 
Font-Names="Calibri" ForeColor="#333333" 
Font-Size="16px" Width="500px" AutoGenerateColumns="true" OnRowCancelingEdit="gvList_RowCancelingEdit" 
OnRowEditing="gvList_RowEditing" OnRowUpdating="gvList_RowUpdating"> 
    <Columns> 
     <asp:TemplateField HeaderText="User Name" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true" ItemStyle-Width="170px"> 
     <ItemTemplate> 
       <asp:Label runat="server" ID="lblUsername" Text='<%# Eval("cUserName") %>'></asp:Label> 
     </ItemTemplate> 
     <EditItemTemplate> 
       <asp:TextBox ID="Editusername" runat="server" Text='<%# Eval("cUserName") %>'></asp:TextBox> 
     </EditItemTemplate> 

     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Dept User" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true" ItemStyle-Width="170px"> 
     <ItemTemplate> 
       <asp:Label runat="server" ID="lblDept" Text='<%# iif(Eval("lDeptUser"),"Yes","No") %>'></asp:Label> 
     </ItemTemplate> 
     <EditItemTemplate> 
       <asp:RadioButtonList ID="RadioButtonList1" runat="server" > 
       <asp:ListItem>Yes</asp:ListItem> 
       <asp:ListItem>No</asp:ListItem> 
       </asp:RadioButtonList> 
      </EditItemTemplate> 
      </asp:TemplateField> 

      <asp:TemplateField HeaderText="Actions" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true" ItemStyle-Width="160px"> 
      <ItemTemplate> 
       <asp:LinkButton ID="btnedit" CommandName="Edit" runat="server" Text="Edit" /> 
       <asp:LinkButton ID="btnDelete" CommandName="Delete" runat="server" Text="Delete" /> 
      </ItemTemplate> 
      <EditItemTemplate> 
       <asp:LinkButton ID="btnUpdate" CommandName="Update" runat="server" Text="Update" /> 
       <asp:LinkButton ID="btnCancel" CommandName="Cancel" runat="server" Text="Cancel" /> 
      </EditItemTemplate> 
        <HeaderStyle Font-Bold="True" ForeColor="White"></HeaderStyle> 
        <ItemStyle Width="120px"></ItemStyle> 
       </asp:TemplateField> 
      </Columns> 
       <EmptyDataTemplate> 
        No records available 
       </EmptyDataTemplate> 
       <HeaderStyle BackColor="#808380" Font-Bold="True" ForeColor="White" VerticalAlign="Top" /> 
       <EditRowStyle BackColor="#2461BF" /> 
       <AlternatingRowStyle BackColor="#E3E3E3" /> 
      </asp:GridView> 

VB

Protected Sub gvList_RowUpdating(sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvList.RowUpdating 

    Dim oldname = DirectCast(gvList.Rows(e.RowIndex).FindControl("lblUsername"), Label) 
    Dim username As String = DirectCast(gvList.Rows(e.RowIndex).FindControl("Editusername"), TextBox).Text 
    Dim tempUser = DirectCast(gvList.Rows(e.RowIndex).FindControl("RadioButtonList1"), RadioButtonList).SelectedIndex 
    Dim query As String = "Update Intranet.dbo.Gn_ISCoordinators SET [email protected]" + 
          ",[email protected] WHERE [email protected]" 

    Using con = New SqlConnection(ConfigurationManager.ConnectionStrings("IntranetConnectionString").ConnectionString) 
     con.Open() 
     Dim cmd = New SqlCommand(query, con) 
     cmd.Parameters.AddWithValue("@uname", username) 
     cmd.Parameters.AddWithValue("@dept", Convert.ToByte(tempUser)) 
     cmd.Parameters.AddWithValue("@oldname", oldname) 
     cmd.ExecuteNonQuery() 
    End Using 

    gvList.EditIndex = -1 
    GetList() 
End Sub 

的問題是在cmd.ExecuteNonQuery()它給了我The parameterized query '(@uname nvarchar(7),@dept tinyint,@oldname nvarchar(4000))Update' expects the parameter '@oldname', which was not supplied.

任何事錯在我的查詢?

+0

如果你還沒有收到任何東西,請檢查你是否使用了page.ispostback if(!Page .IsPostBack){gridviewBIND}'參考http://satindersinght.blogspot.in/2012/08/how-to-addupdate-record-using-gridview.html – 2013-04-10 09:18:36

+0

@Sindersindersh是的,我之前做過。 – 7alhashmi 2013-04-11 04:49:04

+0

你的代碼看起來不錯,但是這行'command.commandtype = commandtype。文本丟失可能是數據問題 – 2013-04-12 05:49:05

回答

2

有在參數

Dim query As String = "Update Intranet.dbo.Gn_ISCoordinators SET [email protected]" + 
         ",[email protected] WHERE [email protected]" 

一個錯字和你有

cmd.Parameters.AddWithValue("@oldname", oldname) 

是不是應該

cmd.Parameters.AddWithValue("@oldName", oldname) 

有大小寫錯誤。

+0

我得到了完全相同的問題! – 7alhashmi 2013-04-08 06:06:39

+0

使用'DirectCast'獲取標籤,文本框和rbl數據有問題嗎? – 7alhashmi 2013-04-08 06:07:42

+0

因爲當我調試時,所有這些值都是空的:S – 7alhashmi 2013-04-08 06:08:32

4

試試這個

Dim oldname as Label =gvList.Rows(e.RowIndex).FindControl("lblUsername") 
    Dim username as TextBox = gvList.Rows(e.RowIndex).FindControl("Editusername") 
    Dim tempUser as RadioButtonList = DirectCast(gvList.Rows(e.RowIndex).FindControl("RadioButtonList1"), RadioButtonList).SelectedIndex 


    Dim query As String = "Update Intranet.dbo.Gn_ISCoordinators SET [email protected]" + 
           ",[email protected] WHERE [email protected]" 
Using con = New SqlConnection(ConfigurationManager.ConnectionStrings("IntranetConnectionString").ConnectionString) 
     con.Open() 
     Dim cmd = New SqlCommand(query, con) 
     cmd.Parameters.AddWithValue("@uname", username.Text) 
     cmd.Parameters.AddWithValue("@dept", Convert.ToByte(tempUser.SelectedIndex)) 
     cmd.Parameters.AddWithValue("@oldname", oldname.Text) 
     cmd.ExecuteNonQuery() 
    End Using 

你必須在@使用oldName使 「N」 資本改變參數的名字,所以因該

你得到的是錯誤

+0

即使在更改之後,我遇到了同樣的問題 – 7alhashmi 2013-04-08 06:09:59

+0

您是否獲取用戶名,tempUser,oldname中的數據? – 2013-04-08 06:13:45

+0

我想獲取數據,但是當我進行調試並檢查用戶名,tempUserm和oldname中的值時,沒有數據! – 7alhashmi 2013-04-08 06:15:17

2

試試這個
代碼看起來不錯,但是這條線command.commandtype = commandtype.text缺少的可能就是這個問題。

也同時傳遞參數Addwithvalue使用username.text上,而不是username同其他控件太

Dim oldname As Label = DirectCast(gvList.Rows(e.RowIndex).FindControl("lblUsername"), Label) 
Dim username As TextBox = DirectCast(gvList.Rows(e.RowIndex).FindControl("Editusername"), TextBox).Text 
Dim tempUser As RadioButton = DirectCast(gvList.Rows(e.RowIndex).FindControl("RadioButtonList1"), RadioButtonList).SelectedIndex 
Dim query As String = "Update Intranet.dbo.Gn_ISCoordinators SET [email protected], [email protected] WHERE [email protected]" 

Using con = New SqlConnection(ConfigurationManager.ConnectionStrings("IntranetConnectionString").ConnectionString) 
    con.Open() 
    Dim cmd = New SqlCommand(query, con) 
    cmd.commandtype = commandtype.text 
    cmd.Parameters.AddWithValue("@uname", username.text) 
    cmd.Parameters.AddWithValue("@dept", Convert.ToByte(tempUser.selectedvalue)) 
    cmd.Parameters.AddWithValue("@oldname", oldname.text) 
    cmd.ExecuteNonQuery() 
End Using 

gvList.EditIndex = -1 
GetList() 
2

對不起我沒有足夠的代表處發表評論,所以我要在這裏回答...

你爲什麼鑄造標籤以標籤並試圖將其用作字符串?:

Dim oldname as string = DirectCast(gvList.Rows(e.RowIndex).FindControl("lblUsername"), Label).text 

似乎更合適。

2

我不知道這個,但可能是因爲當你試圖點擊編輯按鈕,並且gridview的模式處於編輯模式時,lblUsername標籤將被更改爲Editusername文本框..所以我建議你嘗試將全局在更新或編輯之前將會存儲lblUsername的數據的變量字符串。

+0

如果您嘗試過這種方法,請告知我們..也許您可以將標籤的值存儲在gridview編輯事件中,然後獲取更新後的變量值.. – Clyde 2013-04-17 06:13:10