Click here to Skip to main content
15,910,471 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

1 solution

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. :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900