2011-05-14 48 views
0
Protected Sub GridView1_RowCommand _ 
(sender As Object, e As GridViewCommandEventArgs) _ 
    Handles GridView1.RowCommand 
    If e.CommandName.CompareTo("command") = 0 Then 

     Dim itemID As Integer = Convert.ToInt32(_ 
      GridView1.DataKeys(Convert.ToInt32(e.CommandArgument)).Value) 

     Dim sqlConn As New SqlConnection("connectionString") 

     sqlConn.Open() 

     Dim sqlComm As New SqlCommand("UPDATE itemTable SET property = (property + 1) WHERE id = '" + itemID + "', sqlConn") 
     sqlComm.ExecuteNonQuery() 

     sqlConn.Close() 
    End If 

End Sub 

基本上,它不工作,我不明白它。我一直試圖弄清楚這幾天,現在我看不出我做錯了什麼。正如你所看到的,當用戶點擊相應行的按鈕字段時,我試圖手動更新數據庫中的字段,其值爲(originalvalue)+1。通過Gridview手動更新數據庫ASP.NET(VB)

我想我問的是如果任何人都可以指出任何錯誤,請做,或者如果有更好的方法來做到這一點(無需通過所有的DAL/BLL BS),請告訴我。

非常感謝!

+1

您是否嘗試過使用調試器來查看您的itemID具有的值以及代碼是否可以運行? – patmortech 2011-05-14 04:08:55

+0

抱歉,關於以下語句的noobness,但是當我點擊調試按鈕時,爲什麼Visual Studio的行爲就像我選擇了'在瀏覽器中查看aspx頁面'? 「本地」窗口中也不會顯示任何項目。 – sneak14 2011-05-14 15:34:00

+0

你的sql很容易受到注入攻擊 – 2012-03-14 04:11:04

回答

1

我已經解決了我自己的問題!看代碼:

Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) 

    // If multiple buttons are used in a GridView control, use the 
    // CommandName property to determine which button was clicked. 
    If e.CommandName = "commandname" Then 

     // Convert the row index stored in the CommandArgument 
     // property to an Integer. 
     Dim index = Convert.ToInt32(e.CommandArgument) 

     // Retrieve the row that contains the button clicked 
     // by the user from the Rows collection. 
     Dim row = GridView1.Rows(index) 

     // Calculate the new value. 
     Dim tb = CType(row.FindControl("Label1"), Label) 
     Dim newint As Integer = Convert.ToDouble(tb.Text) + 1 

     // Get the id of the idea. 
     Dim id As Integer = Convert.ToInt32(_ 
     GridView1.DataKeys(index).Value) 

     //Manual update to the database. 
     Dim con As New SqlConnection("connectionstring") 
     Dim cmd As New SqlCommand("UPDATE ideatable SET field = " & (newint.ToString) & " WHERE id = " & (id.ToString), con) 
     con.Open() 
     cmd.ExecuteNonQuery() 
     con.Close() 

     //Refresh the page. 
     Page.Response.Redirect("default.aspx") 

    End If 

End Sub 

並記得設置OnRowCommand="GridView1_RowCommand"在aspx文件的代碼在GridView過,我忘了這一點早! :/

我很高興,謝謝你提供的所有幫助!希望這個答案可以幫助像我這樣陷入困境的人。

0

我認爲這是在你的句子的問題: - 嘗試下面一個在地方的聲明

昏暗sqlComm作爲新的SqlCommand的( 「UPDATE itemTable設置屬性=(產權+ 1)WHERE ID =」 + ITEMID + 「」,sqlConn)

+0

嗨Sanjay,是的,那裏肯定有錯誤。但是,在進行更改後,按鈕不起作用。我得到的只是屏幕的快速刷新,沒有更新我的數據庫值... – sneak14 2011-05-14 15:30:55

0

看着你正在生成的字符串,並嘗試手動執行它,然後會發生什麼表得到UPDATE或不。

因爲我認爲「ItemId」不在你的表中。

嘗試在SQL Server中執行此操作「選擇*從itemTable其中id = ItemId」因爲我認爲itemId不存在於您的表中。

+0

我不明白。 ItemID不能在表格中?我的意思是,我的代碼是不是動態獲取屬於用戶點擊的按鈕行的ItemID?所以在這種情況下,這個按鈕應該存在...除非該聲明實際上是錯誤的,並且不真正返回一個ID?或者說這個語句實際上是一個String值而不是Integer,因爲稍後將其添加到SQLcommand中。對不起,我在這裏咆哮,但只是想出了我的想法。並感謝這個答案,感謝它! – sneak14 2011-05-14 19:49:57

+0

這就是爲什麼我問你,首先檢查你的代碼中ItemID的值是什麼。然後檢查表中的ID是否存在。 – Sanjay 2011-05-16 04:31:22