过程不更新数据库中的数据

时间:2013-08-20 07:21:38

标签: asp.net vb.net

这是我的asp代码:

<body>
     <form id="form1" runat="server">
<div>

    <asp:GridView ID="GridView1" runat="server">

    <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:TemplateField HeaderText="Id" InsertVisible="False">
                <EditItemTemplate> 
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("id") %>'></asp:TextBox>
                </EditItemTemplate> 
                <ItemTemplate> 
                    <asp:Label ID="lblId" runat="server" Text='<%# Bind("id") %>'></asp:Label> 
                </ItemTemplate> 
            </asp:TemplateField> 

            <asp:TemplateField HeaderText="firstname">
                <EditItemTemplate> 
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("firstname") %>'></asp:TextBox>
                </EditItemTemplate> 
                <ItemTemplate> 
                    <asp:Label ID="lbl" runat="server" Text='<%# Bind("firstname") %>'></asp:Label> 
                </ItemTemplate> 
            </asp:TemplateField> 
            </Columns>
    </asp:GridView>

</div>
</form>

这是我的vb代码:

Imports System.Data.SqlClient
Imports System.Data

Partial Class testt
    Inherits System.Web.UI.Page
    Public connectionString As String = ConfigurationManager.ConnectionStrings("TestConnectionString").ConnectionString
    Public cn As SqlConnection = New SqlConnection(connectionString)
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim ds As New DataSet
        Dim dt As New DataTable
        If cn.State = ConnectionState.Closed Then
            cn.Open()
        End If
        Dim command As SqlCommand = New SqlCommand("selecttest", cn)
        command.CommandType = CommandType.StoredProcedure
        Dim da As New SqlDataAdapter(command)
        da.Fill(ds, "testtt")
        dt = ds.Tables(0)
        GridView1.DataSource = dt
        If Not IsPostBack Then
            GridView1.DataBind()
        End If
        If cn.State = ConnectionState.Open Then
            cn.Close()
        End If
    End Sub
    Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles GridView1.RowUpdating
        Dim strPersonID As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("TextBox1"), TextBox).Text
        Dim strLastName As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("TextBox2"), TextBox).Text
        Try
            Dim command As SqlCommand = New SqlCommand("updatetest", cn)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.AddWithValue("@pid", strPersonID)
            command.Parameters.AddWithValue("@pfirstname", strLastName)
            If cn.State = ConnectionState.Closed Then
                cn.Open()
            End If
            command.ExecuteNonQuery()
            MsgBox("1 row updated")
            If cn.State = ConnectionState.Open Then
                cn.Close()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Public Sub GridView1_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
        GridView1.EditIndex = e.NewEditIndex
        If Not IsPostBack Then
            GridView1.DataBind()
        End If
    End Sub
End Class

1-网格中的列是重复的,这意味着我从存储过程中获取所选数据,并在新列中的模板字段中绑定数据,而不是只获得2列我得到4相同的数据,我该如何解决这个问题?

2 - rowupdating事件中没有错误一切正常但数据库中的数据没有更新。请注意,存储过程在SQL中工作正常。 它可能是因为前面提到的重复列...

任何帮助?

谢谢你!

0 个答案:

没有答案