0
votes

when i try to preform Update Profile Page in my project , the query does not work very well . the code on below : Protected Sub btnSubmimt_Click(sender As Object, e As EventArgs) Handles btnSubmimt.Click Dim dt As DataTable = New DataTable() dt = Session("Info")

    Dim connStr As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString

    Dim conn As SqlConnection = New SqlConnection(connStr)
    conn.Open()
    Dim cmd As SqlCommand = New SqlCommand()
    Dim da As SqlDataAdapter = New SqlDataAdapter()

    cmd.CommandType = CommandType.Text
    cmd = New SqlCommand("Update [users] Set  Email = @Email , Role = @Role  , Country= @Country where UserName = @UserName")

    cmd.Parameters.Add(New SqlParameter("@Email", txtEmail.Text.ToString))
    cmd.Parameters.Add(New SqlParameter("@Role", txtRole.Text.ToString))
    cmd.Parameters.Add(New SqlParameter("@Country", DDLCountry.SelectedValue.ToString))
    cmd.Parameters.Add(New SqlParameter("@UserName", Session("UserName")))
    cmd.Connection = conn


    cmd.ExecuteNonQuery()

    conn.Close()



    ModalPopupExtender1.Show()

End Sub
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Dim dt As DataTable = New DataTable()
        dt = Session("Info")
        txtusername.Text = dt.Rows(0)("UserName").ToString
        txtEmail.Text = dt.Rows(0)("Email").ToString
        txtRole.Text = dt.Rows(0)("Role").ToString
        DDLCountry.SelectedItem.Text = dt.Rows(0)("Country").ToString
    End If
End Sub 
1
What is it that doesn't work well? Is there a specific error? - Austin Winstanley
You've done more than most people in actually posting your code, but we need to know what the error message is. It doesn't pop up an exception that says "not working very well" does it? - Seano666
Your code looks good; The only possible error I'm seeing is that Session("Info") don't have a value and you are getting an "Object reference not set to an instance of an object" error message. Just an assumption so providing the real error you're encountering will help. - CurseStacker
Like everyone has already said, code looks good, only obvious suspect is the session variable UserName ... post the error you're getting assuming there is an error message. Session variables, if not set, return "" (null string), so a "Object reference not set to an instance of an object"" error wouldn't be generated, but no data would be saved unless there is actually a username of "" (null string" in the table you're saving data to. Put a break point on the "cmd.Connection = conn" line of code, see what the value of Session("UserName") is. - Prescott Chartier
i mean the Update Query are not working . - Nina

1 Answers

0
votes

You do not need DataTable and SqlDataAdapter as you are not retrieving any data.

I believe you want something like this -

Protected Sub btnSubmimt_Click(sender As Object, e As EventArgs)
    Dim connStr As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
    Dim cmdText As String = "Update [users] Set Email = @Email , Role = @Role, Country= @Country where UserName = @UserName"
    Using conn = New SqlConnection(connStr)
        Using cmd = New SqlCommand(cmdText, conn)
            conn.Open()
            cmd.Parameters.Add(New SqlParameter("@Email", txtEmail.Text))
            cmd.Parameters.Add(New SqlParameter("@Role", txtRole.Text))
            cmd.Parameters.Add(New SqlParameter("@Country", DDLCountry.SelectedValue))
            cmd.Parameters.Add(New SqlParameter("@UserName", Session("UserName")))
            cmd.ExecuteNonQuery()
        End Using
    End Using
    ModalPopupExtender1.Show()
End Sub