User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 401,727 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,119 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting
Views: 11782 | Replies: 6
Closed Thread
Join Date: May 2006
Location: Malaysia
Posts: 6
Reputation: sathiya is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sathiya sathiya is offline Offline
Newbie Poster

Beginner: Insert, Update, Delete & Search records from/to sql database

  #1  
May 9th, 2006
Hi Everyone,
First of all, thanx Mojio for providing me the right code information on connecting sql server database.

As I'm a beginner in ASP.NET, using vb as language. I have created few text boxes on the form as an input to the application. I hav managed to insert the records to the sql, but not managed to search & update the records from sql.

My Code's on Insert Button:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strConn As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\staff.mdf;Integrated Security=True;User Instance=True"
Dim cmd As New SqlCommand("INSERT INTO Staff (NRIC, Name, Address, TelNo)VALUES('" & txtIC.Text & "','" & txtName.Text & "','" & txtAddress.Text & "','" & txtTelNo.Text & "')", New SqlConnection(strConn))
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
End Sub

My Query:
I'm trying to update the records thru the textboxes created. The scenario is, the user will key in his student ID in the enter student ID textbox and click on the search button. Then All the details about him such as name, telNO will be displayed accordingly in other textboxes created.How can I retrieve the data from sql back into the textboxes created and from there i edit and update the changes back to sql. Please advise 10q.

Regards,
Sathiya
AddThis Social Bookmark Button
 
Join Date: Jul 2005
Location: Dallas, TX
Posts: 481
Reputation: campkev is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Beginner: Insert, Update, Delete & Search records from/to sql database

  #2  
May 9th, 2006
here is one way to get the info and display it on the screen. See if this works, if it does, try to "reverse" it to get it to update. Let me know if you need help.

Dim dr As SqlClient.SqlDataReader
        Dim cmd As New SqlClient.SqlCommand("Select * from Students where studentid = " & txtStudentID.txt, New SqlClient.SqlConnection(strConn))
        cmd.Connection.Open()
        dr = cmd.ExecuteReader()
        cmd.Connection.Close()

        txtName.Txt = dr("Name")
        txtTelNo.Txt = dr("TelNo")
 
Join Date: Dec 2004
Posts: 1,590
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 34
Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Beginner: Insert, Update, Delete & Search records from/to sql database

  #3  
May 9th, 2006
sathiya: please use code tags when posting code.
 
Join Date: Jul 2005
Location: Dallas, TX
Posts: 481
Reputation: campkev is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Beginner: Insert, Update, Delete & Search records from/to sql database

  #4  
May 9th, 2006
code tags look like this

<[>code]
[/code]
minus the greater than/ less than symbols
 
Join Date: May 2006
Location: Malaysia
Posts: 6
Reputation: sathiya is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sathiya sathiya is offline Offline
Newbie Poster

Re: Beginner: Insert, Update, Delete & Search records from/to sql database

  #5  
May 9th, 2006
Thanx 4 ur info campkev. I hav tried the codes, it's still showing the msg "Invalid attempt to MetaData when reader is closed" under the InvalidOperationException was unhandled by user code dialog box. It was pointing the
txtName.Text = dr("Name")
txtAddress.Text = dr("Address")

I hav tried to write the above code like below after that:
cmd.Connection.Open() n
dr = cmd.ExecuteReader()
txtName.Text = dr("Name")
txtAddress.Text = dr("Address")
cmd.Connection.Close()

This time it's showing the msg "Invalid attempt to read when no data is present" under the InvalidOperationException was unhandled by user code dialog box by pointing the
txtName.Text = dr("Name")
txtAddress.Text = dr("Address")

Please advice.
 
Join Date: Jul 2005
Location: Dallas, TX
Posts: 481
Reputation: campkev is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Beginner: Insert, Update, Delete & Search records from/to sql database

  #6  
May 10th, 2006
what does your sql query look like?
 
Join Date: May 2006
Location: Malaysia
Posts: 6
Reputation: sathiya is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sathiya sathiya is offline Offline
Newbie Poster

Re: Beginner: Insert, Update, Delete & Search records from/to sql database

  #7  
May 10th, 2006
Originally Posted by campkev
what does your sql query look like?

Dim cmd As New SqlClient.SqlCommand("Select * from Staff where NRIC = '" & txtSearch.Text & "'", New SqlClient.SqlConnection(strConn))


The query should look like this when it runs:

Select * from Staff where NRIC = '840225-145035'

and I have a record sitting on the db. Thank you
 
Closed Thread

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb ASP.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the ASP.NET Forum

All times are GMT -4. The time now is 9:17 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC