Click here to Skip to main content
15,917,862 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello

i work with stored procedure.

when i want delete multiple rows from database with stored procedure I deal with the error can not convert string to int32

my code is
C#
protected void Button1_Click(object sender, EventArgs e)
    {
        string id = string.Empty;
        int i=0;
        foreach (GridDataItem mm in grdAllUsers.Items)
        {

            if (mm.Selected)
            {
                id += mm.GetDataKeyValue("usr_Id") + ",";
            }
            

        }

        string strid = id.Substring(0, id.LastIndexOf(","));

        Admin delet = new Admin();
        string req = delet.DeleteUserFromAdmin(strid);
        LoadGridAllUsers();
        Literal1.Text = strid + "/n REquest:" + req;

    }



 internal string DeletUserFromAdmin(string id)
    {
        
            
                SqlCommand cmd = new SqlCommand("DELETE FROM [dbo].[Users] WHERE usr_Id in ("+ id +") ", con);
                //cmd.CommandType = CommandType.StoredProcedure;

                //cmd.Parameters.Add("@userid", SqlDbType.Int).Value = id;

                try
                {

                    if (con.State == ConnectionState.Closed)
                    {
                        con.Open();
                    }

                    if (cmd.ExecuteNonQuery() > 0)
                    {
                        return "Succ";
                    }
                    else
                    {
                        return "UnSucc";
                    }
                }

                catch (Exception ex)
                {

                    return ex.Message;
                }
                finally
                {
                    con.Close();
                }
             
            }


and stored procedure code is:
SQL
USE [MyWebsite]
GO
/****** Object:  StoredProcedure [dbo].[DeletUserFromAdmin]    Script Date: 3/23/2013 9:33:03 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER proc [dbo].[DeletUserFromAdmin]

@userid int


as

DELETE FROM [dbo].[Users]
      WHERE usr_Id in (@userid) 



usr)Id feild is int and dont change it because it is a id of my table and create auto with sql server.
Posted
Comments
[no name] 23-Mar-13 17:12pm    
In the code that you have posted here, you are not calling a stored procedure. And, you are using a string as if it were an integer in your query. Strings are strings and are not integers even if they contains digit characters. Exactly what the error message is telling you. Either use your stored procedure or parse the string to a integer.

1 solution

The reason you are getting error as you are passing a string in "IN " clause and run the sql directly.

Instead use dynamic query like
When you use in clause, then there must be a possibility of deleting rows for multiple ids.
So pass the userid like "1.4.7" from C#

ALTER proc [dbo].[DeletUserFromAdmin]
@userid vrachar(max)
as
  declare @sql varchar(max)='';
  set @sql = 'DELETE FROM [dbo].[Users] WHERE usr_Id in (' + @userid + ') ';
  execute (@sql);


Even you can use parameterized dynamic query for getting rid of sql injection.

Hope this helps.
Cheers
 
Share this answer
 

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