在asp.net中,如何使用文本框更新数据库中的数据

时间:2015-03-15 17:44:36

标签: asp.net vb.net sql-server-2008

我尝试过使用此代码,但它不起作用,

它显示此错误错误更新DataObject引用未设置为对象的实例。

当我尝试更新时,请看看我在这里做错了什么 感谢

Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
    Dim connString As String
    connString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\user\Documents\Visual Studio 2010\WebSites\boomwebsite\App_Data\playaazaleaDatabase.mdf;Integrated Security=True;User Instance=True"
    Dim cmd As SqlCommand

    Dim myConnection As New Data.SqlClient.SqlConnection(connString)
    Try
        myConnection.Open()

        cmd.CommandText = "(UPDATE members SET fname=@FNAME, lname=@LNAME, uname=@UNAME, pword=@PWORD WHERE id=@MemberID)"
        cmd.Connection = myConnection
        cmd.Parameters.AddWithValue("@MemberID", txtboxID.Text)
        cmd.Parameters.AddWithValue("@FNAME", txtboxFname.Text)
        cmd.Parameters.AddWithValue("@LNAME", txtboxLname.Text)
        cmd.Parameters.AddWithValue("@UNAME", txtboxUname.Text)
        cmd.Parameters.AddWithValue("@PWORD", txtboxPword.Text)


        cmd.ExecuteNonQuery()
        Response.Redirect("adminuser.aspx")


    Catch ex As Exception
        lblMessage.Text = "Error Updating Data" & ex.Message
    Finally
        myConnection.Close()

    End Try

1 个答案:

答案 0 :(得分:0)

Dim cmd As SqlCommand替换为

Dim cmd As New SqlCommand()