当我尝试在我的项目中执行更新配置文件页面时,查询不能很好地工作。 下面的代码: 受保护的子btnSubmimt_Click(发件人为对象,e为EventArgs)处理btnSubmimt.Click Dim dt As DataTable = New DataTable() dt =会话("信息")
Dim connStr As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Dim conn As SqlConnection = New SqlConnection(connStr)
conn.Open()
Dim cmd As SqlCommand = New SqlCommand()
Dim da As SqlDataAdapter = New SqlDataAdapter()
cmd.CommandType = CommandType.Text
cmd = New SqlCommand("Update [users] Set Email = @Email , Role = @Role , Country= @Country where UserName = @UserName")
cmd.Parameters.Add(New SqlParameter("@Email", txtEmail.Text.ToString))
cmd.Parameters.Add(New SqlParameter("@Role", txtRole.Text.ToString))
cmd.Parameters.Add(New SqlParameter("@Country", DDLCountry.SelectedValue.ToString))
cmd.Parameters.Add(New SqlParameter("@UserName", Session("UserName")))
cmd.Connection = conn
cmd.ExecuteNonQuery()
conn.Close()
ModalPopupExtender1.Show()
End Sub
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dt As DataTable = New DataTable()
dt = Session("Info")
txtusername.Text = dt.Rows(0)("UserName").ToString
txtEmail.Text = dt.Rows(0)("Email").ToString
txtRole.Text = dt.Rows(0)("Role").ToString
DDLCountry.SelectedItem.Text = dt.Rows(0)("Country").ToString
End If
End Sub
答案 0 :(得分:0)
您不需要 DataTable 和 SqlDataAdapter ,因为您没有检索任何数据。
我相信你想要这样的东西 -
Protected Sub btnSubmimt_Click(sender As Object, e As EventArgs)
Dim connStr As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Dim cmdText As String = "Update [users] Set Email = @Email , Role = @Role, Country= @Country where UserName = @UserName"
Using conn = New SqlConnection(connStr)
Using cmd = New SqlCommand(cmdText, conn)
conn.Open()
cmd.Parameters.Add(New SqlParameter("@Email", txtEmail.Text))
cmd.Parameters.Add(New SqlParameter("@Role", txtRole.Text))
cmd.Parameters.Add(New SqlParameter("@Country", DDLCountry.SelectedValue))
cmd.Parameters.Add(New SqlParameter("@UserName", Session("UserName")))
cmd.ExecuteNonQuery()
End Using
End Using
ModalPopupExtender1.Show()
End Sub