使用vb.net代码的asp.net页中的更新命令不起作用 [英] Not work the update command in asp.net page using vb.net code

查看:79
本文介绍了使用vb.net代码的asp.net页中的更新命令不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,

我正在使用Asp.net2005.我在下面发布我的代码.当我执行此代码时,它将无法正常工作(无法正确更新).请调查一下,并帮助我哪里出错了.

导入System.Data
导入System.IO
导入System.Data.SqlClient

局部类changepassword
继承System.Web.UI.Page

Dim con As SqlConnection
昏暗的cmd作为SqlCommand
昏暗的adp作为SqlDataAdapter
Dim ds As DataSet
昏暗的阅读器,如SqlDataReader

< pre lang ="vb">受保护的子Button1_Click(ByVal发送者作为对象,ByVal e作为System.EventArgs)处理Button1.Click</pre>

< pre lang ="vb"> cmd = New SqlCommand(&< b> select * from ureg where uname =``& quot;& amp; TextBox1.Text&& quot; ;''和upass =''&''& amp; TextBox2.Text& amp;''&'',con</b>)
昏暗的阅读器,如SqlDataReader
reader = cmd.ExecuteReader()
如果reader.Read()= True,那么
cmd =新的SqlCommand(& quot; b>更新ureg SET uname =& quot;& amp; TextBox1.Text& amp;" ;, upass =& quot;& amp; TextBox3.文本& quot ;、 cpass =& quot& amp; amp; amp; amp; amp; amp; quot;文本框4" ;&''''&'',con</b>)
cmd.ExecuteNonQuery()
Response.Write(& quot; changed& quot;)
其他

TextBox1.Text =&&"
TextBox2.Text =&&"
TextBox3.Text =&&"
TextBox4.Text =&& quot;
如果结束
reader.Close()
con.Close()</pre>
结束子

< pre lang ="vb">受保护的子Page_Load(ByVal发送者作为对象,ByVal e作为System.EventArgs)处理Me.Load
con =新的SqlConnection(&数据源= gopi;数据库=医院;集成安全性= True")
con.Open()


结束子
结束类</pre>



谢谢和问候,
Gopi A

Hi Friends,

I am using Asp.net 2005. I post my code in below. While i executed this code it will not work (not update properly). Please look into this and help me where i am in wrong.

Imports System.Data
Imports System.IO
Imports System.Data.SqlClient

Partial Class changepassword
Inherits System.Web.UI.Page

Dim con As SqlConnection
Dim cmd As SqlCommand
Dim adp As SqlDataAdapter
Dim ds As DataSet
Dim reader As SqlDataReader

<pre lang="vb">Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click</pre>

<pre lang="vb">cmd = New SqlCommand(&quot;<b>select * from ureg where uname = ''&quot; &amp; TextBox1.Text &amp; &quot;'' and upass = ''&quot; &amp; TextBox2.Text &amp; &quot;''&quot;, con</b>)
Dim reader As SqlDataReader
reader = cmd.ExecuteReader()
If reader.Read() = True Then
cmd = New SqlCommand(&quot;<b>update ureg SET uname = &quot; &amp; TextBox1.Text &amp; &quot;, upass = &quot; &amp; TextBox3.Text &amp; &quot; , cpass = &quot; &amp; TextBox4.Text &amp; &quot; where uname = '' &quot; &amp; TextBox1.Text &amp; &quot; '' &quot;, con</b>)
cmd.ExecuteNonQuery()
Response.Write(&quot;changed&quot;)
Else

TextBox1.Text = &quot;&quot;
TextBox2.Text = &quot;&quot;
TextBox3.Text = &quot;&quot;
TextBox4.Text = &quot;&quot;
End If
reader.Close()
con.Close()</pre>
End Sub

<pre lang="vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
con = New SqlConnection(&quot;Data Source=gopi;database=Hospital;Integrated Security=True&quot;)
con.Open()


End Sub
End Class</pre>



Thanks and Regards,
Gopi A

推荐答案

似乎您正在将upass字段在读取时设置为textbox2,然后在更新时设置为textbox3.您是根据错误的字段更新的问题吗?如果不行,请详细说明它是如何工作的.是否更新了错误的记录?还是根本不更新?您是否收到错误消息?

以下是我认为在查看您的代码后可能会对您有所帮助的其他提示:

在实际提取数据之前,您不应该在按钮单击事件之前打开连接.在该按钮中单击打开并关闭它.

如果您知道要更新的内容,并且一开始不需要从数据库中提取信息,则可以跳过阅读器部分.您需要做的只是使用update语句设置命令,然后以非查询方式运行该命令.您根本不需要读取器对象.

您应该使用参数进行研究,而不是仅使用< code>"myfield ="& txtMyField.Text</code>.否则,SQL注入会遇到巨大的问题.

希望这会有所帮助.

另外,当您发布问题时,请先预览.您的格式设置无效,并且很难阅读代码.我本打算尝试修复它,但担心我会搞砸. :)
It looks like you are setting the upass field to textbox2 when reading it and then textbox3 when updating. Is the problem that you are updating based on the wrong field there? If not, please explain more about how it isn''t working. Is it updating the wrong record? Or is it not updating at all? Are you getting an error message?

Here are some other tips I thought might help you after looking at your code:

You shouldn''t open the connection until the button click event when you are actually pulling the data. Open and close it within that button click.

If you know what you want to update and you don''t need to pull information from your database at first, you can skip the reader portion. All you need to do is setup the command with the update statement and run that command non query. You don''t need the reader object at all.

You should research using parameters instead of just setting up statements with code like <code>"myfield=" & txtMyField.Text</code>. You can have huge problems with SQL injection otherwise.

Hope this helps.

Also, when you post a question please preview it first. Your formatting didn''t work and it is really hard to read your code. I was going to try to fix it but was afraid I''d mess up. :)


这篇关于使用vb.net代码的asp.net页中的更新命令不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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