Aktham Mahmoud

Aktham Mahmoud

  • NA
  • 720
  • 38.1k

What is a best way to convert from string to GUID?

Jul 16 2022 6:15 PM

I have dropdownlist carry GUID value, when I need to modify user role, I can not able to update user info, because many problems in below, what is a best way to convert from string to GUID?
===========================================

Method 1
Error:Incorrect syntax near '('.

protected void GV_UsersRoles_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            DropDownList DRL_Role = (DropDownList)GV_UsersRoles.Rows[e.RowIndex].FindControl("DDL_Roles");
            try
            {
                string roleId = DRL_Role.SelectedValue.ToString();
                using (SqlConnection sqlcon = new SqlConnection(connString))
                {
                    sqlcon.Open();
                    string Qupdate = "UPDATE Staff SET (RoleId) VALUES(@RoleId) WHERE UserId = @UserId";
                    SqlCommand sqlcmd = new SqlCommand(Qupdate, sqlcon); 
                    sqlcmd.Parameters.Add("@RoleId", SqlDbType.UniqueIdentifier).Value = Guid.Parse(roleId);
                    sqlcmd.ExecuteNonQuery();
                    GV_UsersRoles.DataBind();
                    LB_StError.Text = "Employee Role has been updated Successfully.";
                    LB_StError.ForeColor = System.Drawing.Color.Green;
                    LB_StError.BorderColor = System.Drawing.Color.Green;
                }//End Using 
            }//End try
            catch (Exception ex)
            {

                LB_StError.Text = ex.Message;
                LB_StError.ForeColor = System.Drawing.Color.Red;
                LB_StError.BorderColor = System.Drawing.Color.Red;
            }
        }//End protected void GV3_RowUpdating

===========================================
Method 2
Error:Incorrect syntax near '('.

try
            {
                using (SqlConnection sqlcon = new SqlConnection(connString))
                {
                    sqlcon.Open();
                    string Qupdate = "UPDATE Staff SET (RoleId) VALUES(@RoleId) WHERE UserId = @UserId";
                    SqlCommand sqlcmd = new SqlCommand(Qupdate, sqlcon); 
                    sqlcmd.Parameters.Add(new SqlParameter("@RoleId", SqlDbType.UniqueIdentifier)).Value = new System.Data.SqlTypes.SqlGuid(DRL_Role.SelectedValue);
                
                    sqlcmd.ExecuteNonQuery();
                    GV_UsersRoles.DataBind();
                    LB_StError.Text = "Employee Role has been updated Successfully.";
                    LB_StError.ForeColor = System.Drawing.Color.Green;
                    LB_StError.BorderColor = System.Drawing.Color.Green;
                }//End Using 

===========================================
Method 3
Error:Failed to convert parameter value from a String to a Guid.

using (SqlConnection sqlcon = new SqlConnection(connString))
                {
                    sqlcon.Open();
                    string Qupdate = "UPDATE Staff SET (RoleId) VALUES(@RoleId) WHERE UserId = @UserId";
                    SqlCommand sqlcmd = new SqlCommand(Qupdate, sqlcon);             
                    sqlcmd.Parameters.Add("@RoleId", SqlDbType.UniqueIdentifier).Value = (DRL_Role.SelectedValue.ToString());
                    sqlcmd.ExecuteNonQuery();
                    GV_UsersRoles.DataBind();
                    LB_StError.Text = "Employee Role has been updated Successfully.";
                    LB_StError.ForeColor = System.Drawing.Color.Green;
                    LB_StError.BorderColor = System.Drawing.Color.Green;
                }//End Using 

 


Answers (2)