SQL DB Retrieve then Update

arya

New member
Joined
Jan 23, 2006
Messages
3
Programming Experience
3-5
Hello,

I have created a web form, where I am retrieving a set of recordsvfrom the DB. The data is bound to text-fields. Users update the data. On the button-click the data is supposed to get updated.

Superficially this works fine, the command gets executed, 1 row is also affected, but with the old records.



The code for retrieving data-
VB.NET:
 [INDENT]Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
Dim empid As String
empid = Request.QueryString("EmpID")
If empid <> "" Then
Data_Load(empid)
End If
End Sub
 
Public Sub Data_Load(ByVal empid As String)
con.Open()
ad = New SqlDataAdapter("GetEmployeeDetails '" & empid & "'", con)
dt = New DataTable
ds = New DataSet
ad.Fill(dt)
ds.Tables.Add(dt)
con.Close()
TextBox1.Enabled = False
TextBox1.Text = dt.Rows.Item(0).Item(0)
TextBox7.Text = dt.Rows.Item(0).Item(1)
TextBox2.Text = dt.Rows.Item(0).Item(2)
DropDownList1.SelectedValue = dt.Rows.Item(0).Item(3)
TextBox3.Text = dt.Rows.Item(0).Item(4)
TextBox4.Text = dt.Rows.Item(0).Item(5)
TextBox5.Text = dt.Rows.Item(0).Item(6)
TextBox6.Text = dt.Rows.Item(0).Item(7)
End Sub
 

[/INDENT]Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
'Put user code to initialize the page here
Dim empid As String
empid = Request.QueryString("EmpID")
If empid <> "" Then
Data_Load(empid)
End If
End Sub
Public Sub Data_Load(ByVal empid As String)
con.Open()
ad = New SqlDataAdapter("GetEmployeeDetails '" & empid & "'", con)
dt = New DataTable
ds = New DataSet
ad.Fill(dt)
ds.Tables.Add(dt)
con.Close()
TextBox1.Enabled = False
TextBox1.Text = dt.Rows.Item(0).Item(0)
TextBox7.Text = dt.Rows.Item(0).Item(1)
TextBox2.Text = dt.Rows.Item(0).Item(2)
DropDownList1.SelectedValue = dt.Rows.Item(0).Item(3)
TextBox3.Text = dt.Rows.Item(0).Item(4)
TextBox4.Text = dt.Rows.Item(0).Item(5)
TextBox5.Text = dt.Rows.Item(0).Item(6)
TextBox6.Text = dt.Rows.Item(0).Item(7)
End Sub

The code for editing data on the same page-
VB.NET:
Dim com As New SqlCommand 
Dim ctr As Integer
com = New SqlCommand("UpdateEmployeeDetails", con)
com.CommandType = CommandType.StoredProcedure
com.Parameters.Add("@iEmpID", TextBox1.Text)
com.Parameters.Add("@iName", TextBox7.Text)
com.Parameters.Add("@iAddress", TextBox2.Text)
com.Parameters.Add("@iDesignation",DropDownList1.SelectedItem.Text)
com.Parameters.Add("@iBranchID", TextBox3.Text)
com.Parameters.Add("@iEmail", TextBox4.Text)
com.Parameters.Add("@iPhone", TextBox5.Text)
com.Parameters.Add("@iPassword", TextBox6.Text)
con.Open()
ctr = com.ExecuteNonQuery()
con.Close()

Please help!!
 
Last edited by a moderator:

fomhoire

Member
Joined
Jul 5, 2006
Messages
18
Location
cambs UK
Programming Experience
1-3
Quick thought are you using page.IsPostBack to chatch the submit anywhere. if not the page is likly writing over the contence of the text boxes before writing to the database.
 
Top Bottom