简体   繁体   中英

record update in different row vb.net

I have a stored procedure that works perfectly when updating in sql server. The problem here is that when execute to vb.net. Records of different row is updated. From the image shown,For instance i have selected the studentID 383 in datagridview , student 382 will be updated. and if i selected student 380 ,student 379 will be updated and so on.

数据网格视图

stored procedure code:

ALTER PROCEDURE [dbo].[uspUpdate] 
-- Add the parameters for the stored procedure here 
  @SurName     NVARCHAR(20), 
  @FirstName   NVARCHAR(20), 
  @middleName  NVARCHAR(20), 
  @StudAddress NVARCHAR(20), 
  @Birthday    DATE, 
  @Gender      NVARCHAR(20), 
  @Nationality NVARCHAR(20), 
  @BirthPlace  NVARCHAR(20), 
  @TelNum      NVARCHAR(20), 
  @SWG         NVARCHAR(20), 
  @DWG         DATE , 
  @SLA         NVARCHAR(20), 
  @Note        NVARCHAR(20), 
  @StudPic IMAGE , 
  @FFirstName  NVARCHAR(20), 
  @FLastName   NVARCHAR(20), 
  @FMI         NVARCHAR(20), 
  @FOccupation NVARCHAR(20), 
  @FTelNum     NVARCHAR(20), 
  @MFirstName  NVARCHAR(20), 
  @MLastName   NVARCHAR(20), 
  @MMI         NVARCHAR(20), 
  @MOccupation NVARCHAR(20), 
  @MTelNum     NVARCHAR(20), 
  @CFirstName  NVARCHAR(20), 
  @CLastName   NVARCHAR(20), 
  @CMI         NVARCHAR(20), 
  @CAddress    NVARCHAR(20), 
  @CTelNum     NVARCHAR(20), 
  @CMobile     NVARCHAR(20), 
  @studID      INT 
AS 
  BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
    SET nocount ON; 
    BEGIN try 
      BEGIN TRAN 
      UPDATE parentinformation 
      SET    father_firstname = @FFirstName, 
             father_lastname = @FLastName, 
             father_mi = @FMI, 
             father_occupation = @FOccupation, 
             father_telnum = @FTelNum , 
             mother_firstname = @MFirstName, 
             mother_lastname = @MLastName, 
             mother_mi = @MMI, 
             mother_occupation = @MOccupation, 
             mother_telnum = @MTelNum, 
             contact_firstname = @CFirstName , 
             contact_lastname = @CLastName, 
             contact_mi = @CMI, 
             contact_mobile = @CMobile, 
             contact_telnum = @CTelNum 
      FROM   parentinformation PI, 
             studentinformation SI 
      WHERE  pi.parentid = si.parentid 
      AND    pi.parentid = @studID 
      UPDATE studentinformation 
      SET    surname = @SurName, 
             firstname = @FirstName, 
             middlename = @middleName, 
             studaddress =@StudAddress, 
             birthday = @Birthday, 
             gender = @Gender, 
             nationality = @Nationality, 
             birthplace = @BirthPlace, 
             telnum = @TelNum, 
             schoolwheregraduated = @SWG, 
             dateswhengraduated = @DWG, 
             schoollastattended = @SLA, 
             note = @Note, 
             studimage = @StudPic 
      FROM   parentinformation PI, 
             studentinformation SI 
      WHERE  pi.parentid = si.parentid 
      AND    pi.parentid = @studID 
      COMMIT TRAN 
    END try

vb.net code when executing the update statement

  Using cmd As New SqlClient.SqlCommand("dbo.uspUpdate", cn)
            cmd.Parameters.AddWithValue("@StudID", frmView.dgv1.SelectedCells(0).Value)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.Add("@SurName", SqlDbType.VarChar, 100).Value = txtStudLN.Text
            cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 100).Value = txtStudFN.Text
            cmd.Parameters.Add("@middleName", SqlDbType.VarChar, 100).Value = txtStudMN.Text
            cmd.Parameters.Add("@StudAddress", SqlDbType.VarChar, 100).Value = txtAddress.Text
            cmd.Parameters.Add("@BirthDay", SqlDbType.VarChar, 100).Value = dtpBirthday.Text
            cmd.Parameters.Add("@Gender", SqlDbType.VarChar, 100).Value = Male
            cmd.Parameters.Add("@Nationality", SqlDbType.VarChar, 100).Value = cboNationality.Text
            cmd.Parameters.Add("@BirthPlace", SqlDbType.VarChar, 100).Value = txtPlaceOfBirth.Text
            cmd.Parameters.Add("@TelNum", SqlDbType.VarChar, 100).Value = txtStudentCP.Text
            cmd.Parameters.Add("@SWG", SqlDbType.VarChar, 100).Value = txtSWG.Text
            cmd.Parameters.Add("@DWG", SqlDbType.VarChar, 100).Value = dtpDWG.Text
            cmd.Parameters.Add("@SLA", SqlDbType.VarChar, 100).Value = txtSLA.Text
            cmd.Parameters.Add("@Note", SqlDbType.VarChar, 100).Value = txtNote.Text
            cmd.Parameters.Add("@FFirstName", SqlDbType.VarChar, 100).Value = txtFatherGN.Text
            cmd.Parameters.Add("@FLastName", SqlDbType.VarChar, 100).Value = txtFatherLN.Text
            cmd.Parameters.Add("@FMI", SqlDbType.VarChar, 100).Value = txtFatherMI.Text
            cmd.Parameters.Add("@FOccupation", SqlDbType.VarChar, 100).Value = txtFatherOccupation.Text
            cmd.Parameters.Add("@FTelNum", SqlDbType.VarChar, 100).Value = txtFatherCP.Text
            cmd.Parameters.Add("@MFirstName", SqlDbType.VarChar, 100).Value = txtMotherGN.Text
            cmd.Parameters.Add("@MLastName", SqlDbType.VarChar, 100).Value = txtMotherLN.Text
            cmd.Parameters.Add("@MMI", SqlDbType.VarChar, 100).Value = txtMotherMI.Text
            cmd.Parameters.Add("@MOccupation", SqlDbType.VarChar, 100).Value = txtMotherOccupation.Text
            cmd.Parameters.Add("@MTelNum", SqlDbType.VarChar, 100).Value = txtMotherCP.Text
            cmd.Parameters.Add("@CFirstName", SqlDbType.VarChar, 100).Value = txtContactGN.Text
            cmd.Parameters.Add("@CLastName", SqlDbType.VarChar, 100).Value = txtContactLN.Text
            cmd.Parameters.Add("@CMI", SqlDbType.VarChar, 100).Value = txtContactMI.Text
            cmd.Parameters.Add("@CAddress", SqlDbType.VarChar, 100).Value = txtContactAddress.Text
            cmd.Parameters.Add("@CTelNum", SqlDbType.VarChar, 100).Value = txtContactTelNum.Text
            cmd.Parameters.Add("@CMobile", SqlDbType.VarChar, 100).Value = txtContactCP.Text
            cmd.Parameters.Add(New SqlClient.SqlParameter("@StudPic", SqlDbType.Image)).Value = IO.File.ReadAllBytes(a.FileName)
            cmd.ExecuteNonQuery()
            MsgBox("Save Updated Successfully")
End using

So after some side discussion it was determined that the joins that looked funny to me from the start were the problem. Student id is not the same as parent id. The parentinformation update needed to be:

UPDATE p
SET p.father_firstname = @FFirstName,
    p.father_lastname = @FLastName,
    ...
    ...
FROM parentinformation p
INNER JOIN studentinformation s
    ON p.parentid = s.parentid
WHERE s.StudentID = @studID;

And the student information requires no join at all, just a simple update where StudentID = @studID;

It is vital that you remember that when representing data from a database table in grids that grids use a zero (0) based array to display records. So for example if you have a sql database in which you have a table whose primary key is of type integer and you set that to auto increment by 1 then as you add records to that table they will gain primary keys along the lines of 1,2,3 etc.

Now when you take that table and add it to a grid and set the leftmost column to show your primary keys you'll end up seeing rows as you do in your illustration above, incremented one by one. HOWEVER , and this is the bit you need to take note of, the grid has actually numbered its own internal row collection starting at 0 and not at 1, so you cannot just take the index value of the grid's row and assume that it will relate to the primary key in your database because in fact it will be out by one.

So looking at your illustration above if you want to update information belonging to the primary key 381 and you select that row its index (the grid row that is) is in fact 380 NOT 381. If you use the grid row index to point to the primary key in your database then you need to add 1 to it;

grid.row(I) +1

Alternatively you need to take the value in the cell in the row you wish to update, convert it to and integer and use that.

It's also important to realise that this will also have bearings on any looping through grid rows that you may do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM