Update Record for Current User RRS feed

  • Question

  • User-1402564948 posted

    Good morning, all!

    This needs to update the record of the current user.

    If I give it a WHERE clause, the upload fails.  If I don't give it a WHERE clause, the upload works but updates ALL the records.

    I get no errors or exceptions.  Please help and thank you.  

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            If (HttpContext.Current.User.Identity.IsAuthenticated) Then
                Dim User As String = HttpContext.Current.User.Identity.Name
            End If
            'Dim user As String = HttpContext.Current.User.ToString
        End Sub

    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs)
                Dim strImageName As String = txtName.Text.ToString()
                If FileUpload1.PostedFile IsNot Nothing AndAlso FileUpload1.PostedFile.FileName <> "" Then
                    Dim strExtension As String = System.IO.Path.GetExtension(FileUpload1.FileName)
                    If (strExtension.ToUpper() = ".JPG") Or (strExtension.ToUpper() = ".GIF") Then
                        ' Resize Image Before Uploading to DataBase
                        Dim imageToBeResized As System.Drawing.Image = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream)
                        Dim imageHeight As Integer = imageToBeResized.Height
                        Dim imageWidth As Integer = imageToBeResized.Width
                        Dim maxHeight As Integer = 480
                        Dim maxWidth As Integer = 640
                        imageHeight = (imageHeight * maxWidth) / imageWidth
                        imageWidth = maxWidth
                        If imageHeight > maxHeight Then
                            imageWidth = (imageWidth * maxHeight) / imageHeight
                            imageHeight = maxHeight
                        End If
                        Dim bitmap As New Bitmap(imageToBeResized, imageWidth, imageHeight)
                        Dim stream As System.IO.MemoryStream = New MemoryStream()
                        bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
                        stream.Position = 0
                        Dim image As Byte() = New Byte(stream.Length) {}
                        stream.Read(image, 0, image.Length)
                        ' Create SQL Connection 
                        Dim con As New SqlConnection()
                        con.ConnectionString = ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString
                        ' Create SQL Command 
                        Dim strCurrentUserId As String = User.Identity.GetUserId()
                        Dim cmd As New SqlCommand()
                        cmd.CommandText = "UPDATE AspNetUsers SET Id = @Id, ImageChild = @ImageChild, ImgName = @ImgName WHERE Id = @Id"
                        cmd.CommandType = CommandType.Text
                        cmd.Connection = con
                        Dim Id As New SqlParameter("@Id", User)
                        Id.Value = Id.ToString()
                        Dim ImgName As New SqlParameter("@ImgName", SqlDbType.VarChar, 50)
                        ImgName.Value = strImageName.ToString()
                        Dim UploadedImage As New SqlParameter("@ImageChild", SqlDbType.Image, image.Length)
                        UploadedImage.Value = image
                        Dim result As Integer = cmd.ExecuteNonQuery()
                        If result > 0 Then
                            lblMessage.Text = "File Uploaded"
                        End If
                    End If
                End If
            Catch exe As Exception
                SettingsUpdatedMessage.Visible = True
                SettingsUpdatedMessage.Text = exe.Message
            End Try
        End Sub

    Friday, March 11, 2016 4:36 PM


  • User-1402564948 posted

    My solution turned out to be this parameter

    myCommand.Parameters.AddWithValue("@Id", User.Identity.GetUserId)

    along with the select and where clauses for Id.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 11, 2016 10:02 PM