Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Private Sub editbtn_Click(sender As System.Object, e As System.EventArgs) Handles editbtn.Click
        If Not CheckEmptyTextbox(GroupBox1) Then
            Exit Sub
        End If

        If stucntcttxt.Text.Length <> 10 Then
            MsgBox("Contact Number Must Be 10 Characters Long", MsgBoxStyle.Information)
            Exit Sub
        End If

        Dim Valid As Boolean
        Try
            Valid = Regex.IsMatch(stuemailtxt.Text, "\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase)
        Catch ex As Exception
        End Try
        If Not Valid Then
            MsgBox("Enter Valid Emailid", MsgBoxStyle.Information)
            Exit Sub
        End If

        'Dim con As New SqlConnection("Data Source=ASHUTOSH-PC\SQLEXPRESS;Initial Catalog=Art Station Management System;Integrated Security=True")
        Dim str As String
        Dim i = CInt(stuidcmb.Text)
        Dim a = CInt(stuagetxt.Text)
        Dim gender As String = String.Empty
        If stumalerd.Checked Then
            gender = "Male"
        ElseIf stufemalerd.Checked Then
            gender = "Female"
        End If

        con.Open()
        str = "Update StudentRegister set sname = @sname, smiddlename = @smiddlename, ssurname = @ssurname, sdob = @sdob, ccoursename = @ccoursename, btbatchid = @btbatchid, sage = @sage, sgender = @sgender,scontact = @scontact,saddress = @saddress,semailid = @semailid, sdatereg=@sdatereg where sstudentid = @sstudentid"
        com = New SqlCommand(str, con)
        com.Parameters.AddWithValue("@sstudentid", stuidcmb.Text)
        com.Parameters.AddWithValue("@sname", stufnametxt.Text)
        com.Parameters.AddWithValue("@smiddlename", stumnametxt.Text)
        com.Parameters.AddWithValue("@ssurname", stulnametxt.Text)
        com.Parameters.AddWithValue("@sdob", birthdate.Value.Date)
        com.Parameters.AddWithValue("@ccoursename", coursenamecmb.Text)
        com.Parameters.AddWithValue("@btbatchid", batchidcmb.Text)
        com.Parameters.AddWithValue("@sage", a)
        com.Parameters.AddWithValue("@sgender", gender)
        com.Parameters.AddWithValue("@scontact", stucntcttxt.Text)
        com.Parameters.AddWithValue("@saddress", stuaddtxt.Text)
        com.Parameters.AddWithValue("@semailid", stuemailtxt.Text)
        com.Parameters.AddWithValue("@sdatereg", regdate.Value.Date)
        com.CommandText = str
        com.ExecuteNonQuery()
        con.Close()
        MsgBox("Record Edited Successfully", MsgBoxStyle.Information)
        Display()
        clear()

    End Sub


What I have tried:

Updating Primary would be right?
Posted

1 solution

You aren't updating the primary key - which you say is sstudentid - you are updating the data for that student, and that student only.
Look at your UPDATE statement (I've taken out the miscelaneous stuff so you can see the important bit):
VB
str = "UPDATE StudentRegister SET sname = @sname, ... , sdatereg=@sdatereg WHERE sstudentid = @sstudentid"
You don't list the sstudentid in the SET Clause, just in the WHERE - and only columns which are specified in the SET clause will be changed. The WHERE clause defines which row(s) will be updated. In this case because your WHERE clause references the primary key, only one row will be changed as primary key values must be unique.
BTW: It's a good idea to use UPPER CASE for SQL keywords and it makes them "stand out" better in a complex SQL statement.
 
Share this answer
 
Comments
Ashutosh Dutondkar 7-Feb-16 4:00am    
I tried putting the sstudentid in SET clause but then too it is not getting update!
OriginalGriff 7-Feb-16 4:09am    
Why are you trying to change the primary key?
That's supposed to be the "basic information" which absolutely identifies a particular student. Changing it is unusual - and if you have other tables which refer to this student then changing it can be very dangerous (or may be forbidden by SQL if you have designed you DB correctly and used a Foreign Key relationship.)
It's also possible that you have defined the sstudentid to be a Identity column, in which case you shouldn't try (or even want) to change it as SQL controls the values and creates a unique value for you.
So why do you want to change it? And what exactly happens when you try?
Ashutosh Dutondkar 7-Feb-16 4:17am    
Thats what i wanted to know that changing Primary Key would be right! When i try to update it jst display the message box "Record Updated" which i have used when the record is updated!
OriginalGriff 7-Feb-16 4:47am    
Well, "right" is not the same as "possible"! :laugh:
You can do it - if you get your message box then SQL did the update - but...
If you do the update:
UPDATE MyTable SET MyColumn = 1234 WHERE MyColumn = 1234
Then technically, it'll do it - but since the column already contained the same value it's a pointless change.
And if you miss out the WHERE clause:
UPDATE MyTable SET MyColumn = 1234
Then *all* rows will get the same value.
And if you do the update to actually change it:
UPDATE MyTable SET MyColumn = 7890 WHERE MyColumn = 1234
Then yes, SQL will do it (as long as there aren't Foreign Key constraints or Identify fields involved) - but is it a good idea? It's like changing your birthday - you will still get presents from your mother on the old date because that's the date she associates with "you" and "birthday". Changing the primary key leaves you open to all sorts of problems because you don't know that all references to the "old" id value have been updated - particularly in a multi-user system like an SQL database.

Why are you trying to do this? What do you hope to achieve?
Ashutosh Dutondkar 7-Feb-16 5:07am    
m trying to do this because i have Batches Table in which their is a BatchID coloumn so if sometime i need to change the batchid then it would be helpfull to me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900