Updating a database table using vb

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 37
Reputation: kodingkarthik is an unknown quantity at this point 
Solved Threads: 0
kodingkarthik kodingkarthik is offline Offline
Light Poster

Updating a database table using vb

 
0
  #1
Jan 1st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 104
Reputation: Aneesh_Argent is an unknown quantity at this point 
Solved Threads: 18
Aneesh_Argent Aneesh_Argent is offline Offline
Junior Poster

Re: Updating a database table using vb

 
0
  #2
Jan 1st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 37
Reputation: kodingkarthik is an unknown quantity at this point 
Solved Threads: 0
kodingkarthik kodingkarthik is offline Offline
Light Poster

Re: Updating a database table using vb

 
0
  #3
Jan 1st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Updating a database table using vb

 
0
  #4
Jan 1st, 2009
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:

  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
I answer pm's.
I answer questions.
I answer quickly.
I answer.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 37
Reputation: kodingkarthik is an unknown quantity at this point 
Solved Threads: 0
kodingkarthik kodingkarthik is offline Offline
Light Poster

Re: Updating a database table using vb

 
0
  #5
Jan 6th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Updating a database table using vb

 
0
  #6
Jan 6th, 2009
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
I answer pm's.
I answer questions.
I answer quickly.
I answer.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC