Click to See Complete Forum and Search --> : Desperately seeking help!!!


franky_25
August 30th, 2004, 02:56 PM
Hey guys,
I need help...
i am creating a web page which can display employee details and modify it.I have no problems displaying data in the grid as well as adding data into the database.
I wanted to modify data from the database,so what i did was create an edit button in the display employee details grid which when clicked would open up a new page which would display all the relevant data in text boxes and other elements.Now heres where the problem occurs when i click on the update button nothing happens...
this is the code that i wrote to display data from grid into text boxes

sub edit_det()
txtEmployeeID.Text = Request.Params("empid")
Dim str As String = "Select * from employees where employeeID= '" & txtEmployeeID.Text & "'"
Dim con As New SqlConnection(constr)
Dim cmd As New SqlCommand(str, con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds, "employees")
txtFirstName.Text = ds.Tables("employees").Rows(0)("FirstName")
txtlastname.Text = ds.Tables("employees").Rows(0)("Lastname")
txtTitleOfCourtesy.Text = ds.Tables("employees").Rows(0)("TitleOfCourtesy")
txtBirthDate.Text = ds.Tables("employees").Rows(0)("BirthDate")
txthiredate.Text = ds.Tables("employees").Rows(0)("hiredate")
txtCity.Text = ds.Tables("employees").Rows(0)("City")
txtPostalCode.Text = ds.Tables("employees").Rows(0)("PostalCode")
txtCountry.Text = ds.Tables("employees").Rows(0)("Country")
txtHomePhone.Text = ds.Tables("employees").Rows(0)("HomePhone")
txttitle.Text = ds.Tables("employees").Rows(0)("Title")
txtAddress.Text = ds.Tables("employees").Rows(0)("Address")
con.Close()
the above code works perfectly alright


and this is the code that I wrote on the click event of the update button...
this part doesnt work...
Dim con As New SqlConnection(constr)
Dim str As String = "Select * from employees"
Dim da As New SqlDataAdapter(str, con)
Dim cmd As New SqlCommandBuilder(da)
Dim ds As New DataSet()
da.Fill(ds, "employees")ds.Tables("employees").Rows(0)("FirstName") = txtFirstName.Text
ds.Tables("employees").Rows(0)("LastName") = txtlastname.Text
....
....
....
da.Update(ds, "employees")
Response.Redirect("employee.aspx"
Please help me....
any help would be appriciated...
Frank

MRutledge
August 30th, 2004, 05:15 PM
it appears that you are attempting to update the data adapter but you are modifying the data in your dataset. It should be ds.Update() not da.Update()

franky_25
August 31st, 2004, 02:55 AM
Hi,
ds.update doesnt work,in fact there is no propery associated with dfs called updateThe problem with the code that i posted was that there is no problem i mean,it doesnt do anything at all not even throw up an error....
Any other ideas anybody???

Andy Tacker
August 31st, 2004, 03:42 AM
In Second part of your code, where you update the data, you should equally specify

Dim str As String = "Select * from employees where employeeID= '" & txtEmployeeID.Text & "'"

franky_25
August 31st, 2004, 09:28 AM
hey andy,
Tried your suggestion,no joy.But what amuses me is that when I assign a constant value say
ds.Tables("employees").Rows(0)("LastName") = 13 in the updation part,It works but the values from the textboxes dont...
any ideas???

anupam kant
August 31st, 2004, 10:11 AM
hey I think thats simple... when you click on the UPDATE button the page gets post backed and two things happen
1) it fetches the data from the database and populate the controls [first code block]
2) it fires the update query with the same values.

So actually an update happens at the database level but with the same values, thats why when you hard-code some other value it gets reflected in the database.

Make sure that the first code block does not execute on UPDATE button click.

franky_25
August 31st, 2004, 01:44 PM
well,anupam trhats exactly what i've done...
the code gets displayed on the when a function gets called on the page load event whereas the updation is done on a the update event of a command button
lemme post the code again....

Hey guys,
I need help...
i am creating a web page which can display employee details and modify it.I have no problems displaying data in the grid as well as adding data into the database.
I wanted to modify data from the database,so what i did was create an edit button in the display employee details grid which when clicked would open up a new page which would display all the relevant data in text boxes and other elements.Now heres where the problem occurs when i click on the update button nothing happens...
this is the code that i wrote to display data from grid into text boxes

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Request.Params("empid") <> "" Then
'txtEmployeeID.Text = Request.Params("empid")
update.Visible = True
Submit.Visible = False
edit_det()
end sub

sub edit_det()
txtEmployeeID.Text = Request.Params("empid")
Dim str As String = "Select * from employees where employeeID= '" & txtEmployeeID.Text & "'"
Dim con As New SqlConnection(constr)
Dim cmd As New SqlCommand(str, con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds, "employees")
txtFirstName.Text = ds.Tables("employees").Rows(0)("FirstName")
txtlastname.Text = ds.Tables("employees").Rows(0)("Lastname")
txtTitleOfCourtesy.Text = ds.Tables("employees").Rows(0)("TitleOfCourtesy")
txtBirthDate.Text = ds.Tables("employees").Rows(0)("BirthDate")
txthiredate.Text = ds.Tables("employees").Rows(0)("hiredate")
txtCity.Text = ds.Tables("employees").Rows(0)("City")
txtPostalCode.Text = ds.Tables("employees").Rows(0)("PostalCode")
txtCountry.Text = ds.Tables("employees").Rows(0)("Country")
txtHomePhone.Text = ds.Tables("employees").Rows(0)("HomePhone")
txttitle.Text = ds.Tables("employees").Rows(0)("Title")
txtAddress.Text = ds.Tables("employees").Rows(0)("Address")
con.Close()
the above code works perfectly alright


and this is the code that I wrote on the click event of the update button...
this part doesnt work...
Sub Submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit.Click
Dim con As New SqlConnection(constr)
Dim str As String = "Select * from employees"
Dim da As New SqlDataAdapter(str, con)
Dim cmd As New SqlCommandBuilder(da)
Dim ds As New DataSet()
da.Fill(ds, "employees")ds.Tables("employees").Rows(0)("FirstName") = txtFirstName.Text

''' This is the suprising part if i do this..
da.Fill(ds, "employees")ds.Tables("employees").Rows(0)("FirstName") = 12 0r anything else like a constant this gets updated in the database

ds.Tables("employees").Rows(0)("LastName") = txtlastname.Text
....
....
....
da.Update(ds, "employees")
Response.Redirect("employee.aspx"


Frank

franky_25
August 31st, 2004, 02:39 PM
hey ppl...
how stupid could i get...
i finallly managed to crack the case...with some help from anupam ofcourse...
what was really happening was that on the button click event the page was getting reloaded and the edit_det function was getting called again and those were the values that were getting stored into the database, the original values that is....
all it required was this..

if not page.ispostback .... on the page load event and it worked...

i spent close to 4 days trying to solve this and all it required was some common sense...
well..i guess life aint a bed of roses and u always learn da hard way...
thanx anyway ppl...
keep up da good work....