943,949 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 1753
  • ASP.NET RSS
Jan 1st, 2009
0

Updating a database table using vb

Expand Post »
Hi all,

I am new to Asp.net programming and i have a requirement to retrieve the values from the database and allow the user to edit the values and then save back thechanged values.
I have written the code for saving the form fields values to a database but now i need to retrieve the saved values and allow the user to change the data according to the Name of the employee and then display the respective values of that particular employee for editing.

Thanks in advance,
Karthik.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
kodingkarthik is offline Offline
37 posts
since Dec 2008
Jan 1st, 2009
0

Re: Updating a database table using vb

One Way of doing it might be like this. Since you havnt mentioned your DB I am using an SqlConnection

Imports System.Data.SqlClient
Dim DBCon As New SqlConnection("MyConnectionString")
DBCon.Open()
Dim Cmd As New SqlCommand("Select FirstName, LastName from tblUsers Where UserID = 1 ", DBCon)
Dim dataReader As SqlDataReader
dataReader = Cmd.ExecuteReader()
If (dataReader.Read()) Then
TextBoxFirstName.Text = dataReader("FirstName").ToString()
TextBoxLastName.Text = dataReader("LastName").ToString()
End If
DBCon.Close()
dataReader.Close()

You can also use SqlDataAdapter or SqlDataReader depending on the context
Last edited by Aneesh_Argent; Jan 1st, 2009 at 8:04 am.
Reputation Points: 16
Solved Threads: 18
Junior Poster
Aneesh_Argent is offline Offline
104 posts
since Dec 2008
Jan 1st, 2009
0

Re: Updating a database table using vb

Hi Aneesh,

Thanks for your reply but we are able to get only the data for ID value 1 but we require according to the name. I have used a drop down list for Name and if the user selects a name then accordingly the values should be displayed.

Thanks in advance,
karthik.
Reputation Points: 10
Solved Threads: 0
Light Poster
kodingkarthik is offline Offline
37 posts
since Dec 2008
Jan 1st, 2009
0

Re: Updating a database table using vb

A simple way would be to retrieve the information for the dropdown. Then when the user selects from the drop down, trigger an event to call the database to retrieve the values based on the dropdown selected value. Then place those values into textboxes, and allow the user to edit them. Once they are done editing, have them click a button that will call a save method on the server, and save the data. It's relatively easy. Follow the example below:

vb Syntax (Toggle Plain Text)
  1. Sub Page_Load()
  2. Dim conn As New SqlConnection("connectionString");
  3. Dim cmd As New SqlCommand("SELECT Name, ID FROM Users", conn);
  4. Dim rdr As SqlDataReader
  5.  
  6. Try
  7. conn.Open();
  8.  
  9. rdr = conn.ExecuteReader()
  10.  
  11. If rdr.HasRows Then
  12. dropdownlist1.DataSource = rdr
  13. dropdownlist1.DataBind()
  14. Else
  15. dropdownlist1.Items.Add(New ListItem("-- No values found --", "-- No values found --"))
  16. End If
  17. Catch ex As SqlException
  18. Response.Write(ex.ToString())
  19. Finally
  20. conn.Close();
  21. End Try
  22. End Sub
  23.  
  24. Sub dropdownlist1_SelectedIndexChanged(s As Object, e As EventArgs) Handles dropdownlist1.SelectedIndexChanged
  25. Dim conn As New SqlConnection("connectionString");
  26. Dim cmd As New SqlCommand("SELECT Name, Address FROM Users WHERE ID=@id", conn);
  27. Dim rdr As SqlDataReader
  28.  
  29. cmd.Parameters.AddWithValue("@id", dropdownlist1.SelectedValue)
  30.  
  31. Try
  32. conn.Open();
  33.  
  34. rdr = conn.ExecuteReader()
  35.  
  36. If rdr.HasRows Then
  37. While rdr.Read()
  38. textbox1.Text = rdr("Name")
  39. textbox2.Text = rdr("Address")
  40. End While
  41. Else
  42. Response.Write("No users found with the given id: " & dropdownlist1.SelectedValue)
  43. End If
  44. Catch ex As SqlException
  45. Response.Write(ex.ToString())
  46. Finally
  47. conn.Close();
  48. End Try
  49. End Sub
  50.  
  51. Sub button1_Click(s As Object, e As EventArgs)
  52. Dim conn As New SqlConnection("connectionString");
  53. Dim cmd As New SqlCommand("UPDATE Users SET Name=@name, Address=@address WHERE ID=@id", conn);
  54. Dim successfulRows As Integer = 0
  55.  
  56. cmd.Parameters.AddWithValue("@name", Trim(textbox1.Text))
  57. cmd.Parameters.AddWithValue("@address", Trim(textbox2.Text))
  58. cmd.Parameters.AddWithValue("@id", dropdownlist1.SelectedValue)
  59.  
  60. Try
  61. conn.Open();
  62.  
  63. successfulRows = cmd.ExecuteNonQuery()
  64.  
  65. If successfulRows > 0 Then
  66. Response.Write("Updated " & successfulRows & " successfully.")
  67. Else
  68. Response.Write("Could not find row to update with id: " & dropdownlist1.SelectedValue)
  69. End If
  70. Catch ex As SqlException
  71. Response.Write(ex.ToString())
  72. Finally
  73. conn.Close();
  74. End Try
  75. End Sub
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007
Jan 6th, 2009
0

Re: Updating a database table using vb

Hi,

Thanks for your reply but i am getting an error at
rdr=conn.ExecuteReader() and the error says that ExecuteReader() is not a member of System.Data.Sqlclient.SqlConnection
can any one please help me out with this problem?

Thanks in advance,
karthik.
Reputation Points: 10
Solved Threads: 0
Light Poster
kodingkarthik is offline Offline
37 posts
since Dec 2008
Jan 6th, 2009
0

Re: Updating a database table using vb

That's because it is not a member of System.Data.SqlClient.SqlConnection. That is the class of the actual connection. You need one right before that:

System.Data.SqlClient
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Using login form control, how to redirect users to different pages?
Next Thread in ASP.NET Forum Timeline: Reporting Services Button problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC