Updating individual fields from a row in LINQ

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mhogshaw
    New Member
    • Apr 2013
    • 1

    Updating individual fields from a row in LINQ

    Hi,

    I am trying to update 3 individual fields from a row in my database using LINQ. I have to enter a student id when the page runs then this matches their record in the database and populates the gridview with the 3 fields that i want to edit. Is their any way to do this? So far i have this,

    Code:
    Protected Sub SubBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubBtn.Click
    
    Dim number = txtStudentID.Text
    
    Dim db As New OrionDataClassesDataContext()
    
    
    Dim match = From u In db.Users
    Where u.User_Number = number
    Select u.Contact_number, u.Home_address, u.Term_address
    
    GridView1.DataSource = match
    GridView1.DataBind()
    
    End Sub
    Any help much appreciated.
    Thanks,
    Michael
    Last edited by Rabbit; Apr 12 '13, 04:07 PM. Reason: Please use code tags when posting code.
  • vijay6
    New Member
    • Mar 2010
    • 158

    #2
    Hey Mhogshaw, Create a new button. Once if that button was clicked then connect to your database and update fields like as follows,


    Code:
    Private Sub Save_Click(sender As Object, e As EventArgs)
    	Try
    		Dim con As New SqlConnection("Your database connection string")
    	
    		For i As Integer = 0 To dataGridView1.Rows.Count - 1
    			
    			Dim cmd As New SqlCommand("UPDATE Users SET 
    			Contact_number = '" + dataGridView1.Rows(i).Cells(0).Value.ToString() + "', 
    			Home_address = '" + dataGridView1.Rows(i).Cells(1).Value.ToString() + "', 
    			Term_address = '" + dataGridView1.Rows(i).Cells(2).Value.ToString() + "' 
    			WHERE User_Number = '" + txtStudentID.Text + "'", con)
    		
    			con.Open()
    			cmd.ExecuteNonQuery()
    		Next
    		
    		MsgBox("Updated")
    	Catch ex As Exception
    		MsgBox(ex.ToString())
    	Finally
    		con.Close()
    	End Try
    End Sub

    Comment

    Working...