ASP净UPDATE命令不工作 [英] ASP Net UPDATE Command not working

查看:106
本文介绍了ASP净UPDATE命令不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在code有以下UPDATE命令(VB编写的)。

I have the following UPDATE command (written in VB) in my code.

Dim currentUser As String = User.Identity.Name

    Dim myConnectionString As String = ConfigurationManager.ConnectionStrings("DBConnection").ConnectionString
    Dim myCommand As New SqlCommand("UPDATE tblProfile SET Title= @Title, FirstName= @FirstName, LastName= @LastName, MiddleName= @MiddleName, HomePhoneNumber= @HomePhoneNumber, MobilePhoneNumber= @MobilePhoneNumber, Address= @Address, StreetName= @StreetName, StreetType= @StreetType, Suburb= @Suburb, PostCode= @PostCode, State= @State WHERE UserName = '" & currentUser & "'", New SqlConnection(myConnectionString))
    myCommand.Connection.Open()
    myCommand.Parameters.AddWithValue("@Title", Title.SelectedItem.Text)
    myCommand.Parameters.AddWithValue("@FirstName", FirstName.Text)
    myCommand.Parameters.AddWithValue("@LastName", LastName.Text)
    If MiddleNames.Text = String.Empty Then
        myCommand.Parameters.AddWithValue("@MiddleName", DBNull.Value)
    Else
        myCommand.Parameters.AddWithValue("@MiddleName", MiddleNames.Text)
    End If
    If HomePhoneNumber.Text = String.Empty Then
        myCommand.Parameters.AddWithValue("@HomePhoneNumber", DBNull.Value)
    Else
        myCommand.Parameters.AddWithValue("@HomePhoneNumber", HomePhoneNumber.Text)
    End If
    If MobilePhoneNumber.Text = String.Empty Then
        myCommand.Parameters.AddWithValue("@MobilePhoneNumber", DBNull.Value)
    Else
        myCommand.Parameters.AddWithValue("@MobilePhoneNumber", MobilePhoneNumber.Text)
    End If
    myCommand.Parameters.AddWithValue("@Address", AddressNumber.Text)
    myCommand.Parameters.AddWithValue("@StreetName", StreetName.Text)
    myCommand.Parameters.AddWithValue("@StreetType", StreetType.SelectedItem.Text)
    myCommand.Parameters.AddWithValue("@Suburb", Suburb.Text)
    myCommand.Parameters.AddWithValue("@PostCode", Postcode.Text)
    myCommand.Parameters.AddWithValue("@State", State.SelectedItem.Text)

    myCommand.ExecuteNonQuery()
    myCommand.Connection.Close()

    Dim myCommandPref As New SqlCommand("UPDATE tblPreferences SET Classical = @Classical, Comedy = @Comedy, Concerts = @Concerts, Dance = @Dance, DiningOut = @DiningOut, Exhibitions = @Exhibitions, Family = @Family, Festivals = @Festivals, Lifestyle = @Lifestyle, Musicals = @Musicals, Opera = @Opera, Rock = @Rock, Sports = @Sports, Theatre = @Theatre WHERE UserName = '" & currentUser & "'", New SqlConnection(myConnectionString))

    myCommandPref.Connection.Open()

    Dim boolClassical As Boolean = Preferences.Items(0).Selected
    myCommandPref.Parameters.AddWithValue("@Classical", boolClassical.ToString)

    Dim boolComedy As Boolean = Preferences1.Items(0).Selected
    myCommandPref.Parameters.AddWithValue("@Comedy", boolComedy.ToString)

    Dim boolConcerts As Boolean = Preferences.Items(1).Selected
    myCommandPref.Parameters.AddWithValue("@Concerts", boolConcerts.ToString)

    Dim boolDance As Boolean = Preferences1.Items(1).Selected
    myCommandPref.Parameters.AddWithValue("@Dance", boolDance.ToString)

    Dim boolDiningOut As Boolean = Preferences.Items(2).Selected
    myCommandPref.Parameters.AddWithValue("@DiningOut", boolDiningOut.ToString)

    Dim boolExhibitions As Boolean = Preferences1.Items(2).Selected
    myCommandPref.Parameters.AddWithValue("@Exhibitions", boolExhibitions.ToString)

    Dim boolFamily As Boolean = Preferences.Items(3).Selected
    myCommandPref.Parameters.AddWithValue("@Family", boolFamily.ToString)

    Dim boolFestivals As Boolean = Preferences1.Items(3).Selected
    myCommandPref.Parameters.AddWithValue("@Festivals", boolFestivals.ToString)

    Dim boolLifestyle As Boolean = Preferences.Items(4).Selected
    myCommandPref.Parameters.AddWithValue("@Lifestyle", boolLifestyle.ToString)

    Dim boolMusicals As Boolean = Preferences1.Items(4).Selected
    myCommandPref.Parameters.AddWithValue("@Musicals", boolMusicals.ToString)

    Dim boolOpera As Boolean = Preferences.Items(5).Selected
    myCommandPref.Parameters.AddWithValue("@Opera", boolOpera.ToString)

    Dim boolRock As Boolean = Preferences1.Items(5).Selected
    myCommandPref.Parameters.AddWithValue("@Rock", boolRock.ToString)

    Dim boolSports As Boolean = Preferences.Items(6).Selected
    myCommandPref.Parameters.AddWithValue("@Sports", boolSports.ToString)

    Dim boolTheatre As Boolean = Preferences1.Items(6).Selected
    myCommandPref.Parameters.AddWithValue("@Theatre", boolTheatre.ToString)

    myCommandPref.ExecuteNonQuery()
    myCommandPref.Connection.Close()

当用户presses这是触发code中的按钮,我的网页只刷新,但不更新数据库中的信息。我环顾四周,有些人说你需要有主键作为',其中'语句,所以我做了'用户名'这两个表中的主键。

When the user presses the button which fires that code, my page simply refreshes, but does not update the information in the database. I have looked around, and some people were saying you needed to have the primary key as the 'where' statement, so I made 'UserName' the primary key in both tables.

可能有人请帮我解决这个问题。

Could someone please help me to fix this.

推荐答案

今天我回到我的code几个月后,经过短暂的搜索过,发现问题...我需要包括'如果没有的IsPostBack然后......我的Page_Load。我每次按钮是pressed,这重置我的页面上的字段重置我的网页,从而发送相同的信息反馈到我的服务器 - 我正在更新我的服务器使用相同的信息。

I went back to my code after a few months today, and after a brief search through, found the problem... I needed to include 'if not IsPostBack then...' to my Page_Load. I was resetting my page each time the button was pressed, which reset the fields on my page, thus sending the same information back to my server - I was updating my server with the same information.

对于同样的问题,任何人,帮我:
http://www.java-samples.com/showtutorial.php?tutorialid=1083

For anyone with the same problem, this helped me: http://www.java-samples.com/showtutorial.php?tutorialid=1083

这篇关于ASP净UPDATE命令不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆