Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This roleid is calling from the database table having fields RoleID, RoleName, ModifiedBY, ModifiedDate

In which all the fields are calling but getting error in the line RoleID in update statement asp.net project. I will share you the code please help me

What I have tried:

// Getting RowIndex of GridView
//GridViewRow gvRow = (GridViewRow)(sender as Control).Parent.Parent;
int index = row.RowIndex;
Guid roleId = (Guid)(gvRoleListDispaly.DataKeys[index].Value);

con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
con.Open();

// Command Statement
SqlCommand cmd1 = new SqlCommand("select RoleID from Security_Roles where RoleID=@RoleID", con);

// RoleID
cmd1.Parameters.Add("@RoleID", SqlDbType.UniqueIdentifier);
cmd1.Parameters["@RoleID"].Value = new Guid(roleId.ToString());

sdr = cmd1.ExecuteReader();            

while (sdr.Read())
{
    if (sdr["RoleID"].ToString() == roleId.ToString())
    {
        sdr.Close();
        // Update Command
        using (cmd = new SqlCommand("UPDATE Security_Roles SET RoleName=@RoleName, RoleID=@RoleID, ModifiedBy=@ModifiedBy, ModifiedOn=@ModifiedOn where RoleID=@RoleID", con))
        {
            // RoleName
            cmd.Parameters.Add("@RoleName", SqlDbType.VarChar, 50);
            cmd.Parameters["@RoleName"].Value = roleName;

            // RoleID
            cmd1.Parameters.Add("@RoleID", SqlDbType.UniqueIdentifier);
            cmd1.Parameters["@RoleID"].Value = new Guid(roleId.ToString());

            // ModifiedBy
            // If the value of UserID is Null then ModifiedBy value will be Null
            // Otherwise ModifiedBy field value will be User who creates it.
            string modifiedBy = userID == Guid.Empty ? null : userID.ToString();
            cmd.Parameters.Add("@ModifiedBy", SqlDbType.UniqueIdentifier);
            cmd.Parameters["@ModifiedBy"].Value = new Guid(modifiedBy.ToString());

            // ModifiedOn
            cmd.Parameters.Add("@ModifiedOn", SqlDbType.DateTime);
            cmd.Parameters["@ModifiedOn"].Value = date;

            cmd.ExecuteNonQuery();
        }
    }
}


Getting error at cmd.ExecuteNonQuery();
Posted
Updated 11-Mar-18 20:11pm

1 solution

That's probably because you have added RoleId parameter to another command object than the one you are using executing UPDATE command.
C#
cmd1.Parameters.Add("@RoleID", SqlDbType.UniqueIdentifier);
cmd1.Parameters["@RoleID"].Value = new Guid(roleId.ToString());

Change it to following-
C#
cmd.Parameters.Add("@RoleID", SqlDbType.UniqueIdentifier);
cmd.Parameters["@RoleID"].Value = new Guid(roleId.ToString());


Hope, it helps :)
 
Share this answer
 
Comments
Member 8583441 12-Mar-18 2:14am    
I already tried the same but no result
Member 8583441 12-Mar-18 2:17am    
Thanks i got the answer..... My mistake is at that line itself again thanks for correcting this line i will accept this answer
Member 8583441 12-Mar-18 2:29am    
My answer is satisfied but getting another error saying "Invalid attempt to call Read when reader is closed." But i also deleted the line sdr.close() method but no use.... Same error
Member 8583441 12-Mar-18 2:33am    
I got the solution for the error Invalid attempt to call Read when reader is closed.

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