Click here to Skip to main content
15,898,949 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more: , +
i have aspnet_Roles
ROLID                           ROLNAME

   9acf74a8-d48b-4541-815e-695117a7a093    Admin
   88299f05-b2aa-41c2-81f5-69959777677c    AltinUye
   5712cd36-406c-490e-9da0-235fcec32186    NormalUye



i have my aspnet_UsersInRoles


 USERID                                 ROLEID
fbd74e8b-f5e6-44a9-9fd4-1930beafb0e3    9acf74a8-d48b-4541-815e-695117a7a093
e64aa732-8631-46e6-90bb-0d2b882a21e3    88299f05-b2aa-41c2-81f5-69959777677c
241e204e-530e-4917-97b6-4a14f7057e1f    88299f05-b2aa-41c2-81f5-69959777677c
ca5999b8-f076-482d-9be4-709efd0ce550    88299f05-b2aa-41c2-81f5-69959777677c
0e6789fe-b6fd-4ac5-8da9-f8eb661ae31f    88299f05-b2aa-41c2-81f5-69959777677c


My stored procedure is

ALTER proc [dbo].[RolDuzenle]
(
@UserId uniqueidentifier,
@RolId   uniqueidentifier,
@Result int output
)
as
Declare @RecordControl  int 
Set @RecordControl=(select count(*) from dbo.aspnet_UsersInRoles where UserId=@UserId and RoleId=@RolId) 
update dbo.aspnet_UsersInRoles set RoleId=@RolId   where UserId=@UserId 
if(@RecordControl=0) 
begin 
set @Result=0
end
else
begin
set @Result=1
end
return @Result



Baglanti baglan = new Baglanti();

          SqlParameter[] paramdizi2 = new SqlParameter[3]
          {
              new SqlParameter(";@UserId",SqlDbType.UniqueIdentifier),
              new SqlParameter("@RolId",SqlDbType.UniqueIdentifier),
              new SqlParameter("@Result",SqlDbType.Int)

          };

          paramdizi2[0].Value = new Guid(TxtUserId.Text);
          paramdizi2[1].Value = new Guid(TxtRolId.Text);
          paramdizi2[2].Direction = ParameterDirection.Output;
          baglan.ExecuteNonQuery("RolDuzenle", CommandType.StoredProcedure, paramdizi2);
          if (paramdizi2[2].Value.ToString() == "0")
          {
              LabelDurum.Text = "Rol Updated";
          }
          else
          {
              LabelDurum.Text = "The role is already same";
          }

ı managed this part.ı did it but;


i want to get the result below


C#
Baglanti baglan = new Baglanti();

          SqlParameter[] paramdizi2 = new SqlParameter[3]
          {
              new SqlParameter("@UserId";,SqlDbType.UniqueIdentifier),
              new SqlParameter("@RolId";,SqlDbType.UniqueIdentifier),
              new SqlParameter("@Result",SqlDbType.Int)

          };

          paramdizi2[0].Value = new Guid(TxtUserId.Text);
          paramdizi2[1].Value = new Guid(TxtRolId.Text);
          paramdizi2[2].Direction = ParameterDirection.Output;
          baglan.ExecuteNonQuery("RolDuzenle";, CommandType.StoredProcedure, paramdizi2);
          if (paramdizi2[2].Value.ToString() == "0")
          {
              LabelDurum.Text = "Role updated";
          }
          else
          {
              LabelDurum.Text = "The table doesnt include this RoleId";
          }

if ı used RolDuzenle stored procedure but, ı dont arrive this result

        //if (paramdizi2[2].Value.ToString() == "0")
         {
             LabelDurum.Text = "Role updated";
         }
         else
         {
             LabelDurum.Text = "The aspnet_Roles table doesnt include this RoleId"
         }
//
İf ı enter false Role Id my program is failed.

SQL
The UPDATE statement conflicted with the FOREIGN KEY constraint "FK__aspnet_Us__RoleI__52593CB8". The conflict occurred in database "TskYurt", table"dbo.aspnet_Roles, column 'RoleId';.
The statement has been terminated.



IS IT POSSIBLE?

i want to this
if (paramdizi2[2].Value.ToString() == "0")
         {
             LabelDurum.Text = "Role updated";
         }
         else
         {
             LabelDurum.Text ="The aspnet_Roles table doesnt include this RoleId";
         }
Posted
Updated 14-Jun-11 15:07pm
v3

1 solution

Before updating the roleid you need to check if that role exists, if it doesn't then return 0 immediately, no need of update statement in that case. As you can see from the error message, it gives a "Foreign Key Constraint" error which is correct behavior.


Here is the modified SP. Just moved the update statement to the else part (when there exist a record with that role):
ALTER proc [dbo].[RolDuzenle]
(
@UserId uniqueidentifier,
@RolId   uniqueidentifier,
@Result int output
)
as
Declare @RecordControl  int 
Set @RecordControl=(select count(*) from dbo.aspnet_UsersInRoles where UserId=@UserId and RoleId=@RolId)
begin 
set @Result=0
end
else
begin 
update dbo.aspnet_UsersInRoles set RoleId=@RolId   where UserId=@UserId 
if(@RecordControl=0) 
set @Result=1
end
return @Result


I hope that helps.
 
Share this answer
 
v2
Comments
Ccsnow 15-Jun-11 3:50am    
ı dont understand pls give a example for me
RakeshMeena 15-Jun-11 5:07am    
Updated the solution!
[no name] 15-Jun-11 4:48am    
Rakesh, Please add more details
Ccsnow 15-Jun-11 5:46am    
ALTER proc [dbo].[RolDuzenle]
(
@UserId uniqueidentifier,
@RolId uniqueidentifier,
@Result int output
)
as
Declare @RecordControl int
Set @RecordControl=(select count(*) from dbo.aspnet_UsersInRoles where UserId=@UserId and RoleId=@RolId)
begin
set @Result=0
end
else
begin
update dbo.aspnet_UsersInRoles set RoleId=@RolId where UserId=@UserId
if(@RecordControl=0)
set @Result=1
end
return @Result

i take error massage

Incorrect syntax near the keyword 'else'.Your sp is faied my friend ı understand but ı dont do that.

thanks for your attention but ı thınk ıt's a big problem

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