| | |
Updating a database table using vb
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2008
Posts: 37
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Dec 2008
Posts: 104
Reputation:
Solved Threads: 18
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
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.
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
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)
Sub Page_Load() Dim conn As New SqlConnection("connectionString"); Dim cmd As New SqlCommand("SELECT Name, ID FROM Users", conn); Dim rdr As SqlDataReader Try conn.Open(); rdr = conn.ExecuteReader() If rdr.HasRows Then dropdownlist1.DataSource = rdr dropdownlist1.DataBind() Else dropdownlist1.Items.Add(New ListItem("-- No values found --", "-- No values found --")) End If Catch ex As SqlException Response.Write(ex.ToString()) Finally conn.Close(); End Try End Sub Sub dropdownlist1_SelectedIndexChanged(s As Object, e As EventArgs) Handles dropdownlist1.SelectedIndexChanged Dim conn As New SqlConnection("connectionString"); Dim cmd As New SqlCommand("SELECT Name, Address FROM Users WHERE ID=@id", conn); Dim rdr As SqlDataReader cmd.Parameters.AddWithValue("@id", dropdownlist1.SelectedValue) Try conn.Open(); rdr = conn.ExecuteReader() If rdr.HasRows Then While rdr.Read() textbox1.Text = rdr("Name") textbox2.Text = rdr("Address") End While Else Response.Write("No users found with the given id: " & dropdownlist1.SelectedValue) End If Catch ex As SqlException Response.Write(ex.ToString()) Finally conn.Close(); End Try End Sub Sub button1_Click(s As Object, e As EventArgs) Dim conn As New SqlConnection("connectionString"); Dim cmd As New SqlCommand("UPDATE Users SET Name=@name, Address=@address WHERE ID=@id", conn); Dim successfulRows As Integer = 0 cmd.Parameters.AddWithValue("@name", Trim(textbox1.Text)) cmd.Parameters.AddWithValue("@address", Trim(textbox2.Text)) cmd.Parameters.AddWithValue("@id", dropdownlist1.SelectedValue) Try conn.Open(); successfulRows = cmd.ExecuteNonQuery() If successfulRows > 0 Then Response.Write("Updated " & successfulRows & " successfully.") Else Response.Write("Could not find row to update with id: " & dropdownlist1.SelectedValue) End If Catch ex As SqlException Response.Write(ex.ToString()) Finally conn.Close(); End Try End Sub
I answer pm's.
I answer questions.
I answer quickly.
I answer.
I answer questions.
I answer quickly.
I answer.
![]() |
Similar Threads
- Formatting text area (PHP)
- Using php and html forms to update a mysql database (PHP)
- Using php and html forms to update a mysql database (Community Introductions)
- code is nopt updating the database as given below (Java)
- Help editing table (Java)
- updating database problem (PHP)
- Beginner: how to add new record into database??? (VB.NET)
- Error connecting to database (ASP.NET)
- Database not being populated.... (Java)
Other Threads in the ASP.NET Forum
- Previous Thread: Using login form control, how to redirect users to different pages?
- Next Thread: Reporting Services Button problem
| Thread Tools | Search this Thread |
.net 3.5 activexcontrol ajax alltypeofvideos appliances asp asp.net bc30451 beginner box browser businesslogiclayer button c# cac checkbox class commonfunctions control countryselector dataaccesslayer database datagrid datagridview datagridviewcheckbox datalist deployment development dgv dialog dropdownlist dropdownmenu dynamic dynamically edit embeddingactivexcontrol expose fileuploader fill findcontrol flash formatdecimal formview gridview gudi iis javascript list listbox login microsoft mouse mssql nameisnotdeclared news novell numerical opera panelmasterpagebuttoncontrols problem radio redirect registration relationaldatabases reportemail save schoolproject search security sessionvariables silverlight smoobjects software sql sql-server sqlserver2005 ssl suse textbox tracking treeview unauthorized validatedate validation vb.net video videos vista visualstudio vs2008 web webapplications webdevelopemnt webdevelopment webprogramming webservice wizard xsl youareanotmemberofthedebuggerusers






