Click here to Skip to main content
15,900,254 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,
I have a stored procedure which is like this,
SQL
Alter proc usp_DelTechnology
(@TechnologyID int)
 as
Begin
 -- make sure we don't have reference descriptions
if(NOT EXISTS(select TechnologyID from tblTechnologySubTypes where TechnologyID = @TechnologyID) and
   NOT EXISTS(select TechnologyID from tblApplicationVsTechnologies where TechnologyID = @TechnologyID) and
   NOT EXISTS(select TechnologyID from tblEffortHours where TechnologyID = @TechnologyID))
BEGIN
 DELETE from tblTechnologies where TechnologyID = @TechnologyID
END
else
 begin
 Print 'False'
 end
 END

I have called the sp like this,
C#
public int DeleteTechnology()
       {
           SqlParameter[] prms = new SqlParameter[1];
           prms[0] = new SqlParameter("@TechnologyID", TechnologyID);

           Data.ExecuteNonQuery("usp_DelTechnology", prms);
           return 0;
       }

Can anyone help me on how to do this in c#. I want to delete the technology if it doesnt have the reference in another table.. if it has then it should show a msg.. I have to do this in grid view image button.. On click of image button, the delete should happen..
Please help.
Thanku. :)
Posted
Updated 22-Apr-13 0:11am
v3
Comments
Naz_Firdouse 22-Apr-13 3:37am    
what have you tried???
Anusha Sridhar 22-Apr-13 3:44am    
As a newbie to .net , I have no idea on how to give that if not exists condition in c#.. so am hanging in the middle struggling what to do.
Naz_Firdouse 22-Apr-13 3:48am    
did you checked whether your SP is working correct... in DB itself by executing the SP
Anusha Sridhar 22-Apr-13 5:51am    
sorry for the late reply.. yes. its working fine with the sp/..

you can have your image button inside the gridview like this ...
ASP.NET
asp:ImageButton runat="server" ID="ibtnDelete"
        Text="delete"
        CommandName="delete"
        CommandArgument='<%#Eval("technologyID")%>
'


and you can add a RowCommand
C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "delete")
    {
        DeleteTechnology(convert.toInt32(e.CommandArgument));
    }
}


C#
public int DeleteTechnology(int TechnologyID)
       {
           SqlParameter[] prms = new SqlParameter[1];
           prms[0] = new SqlParameter("@TechnologyID", TechnologyID);
 
           Data.ExecuteNonQuery("usp_DelTechnology", prms);
           return 0;
       }
 
Share this answer
 
Alter your stored procedure as below, so that it returns some value like below:
SQL
Alter proc usp_DelTechnology
(
   @TechnologyID int
)

AS

BEGIN
 -- make sure we don't have reference descriptions
if(NOT EXISTS(select TechnologyID from tblTechnologySubTypes where TechnologyID = @TechnologyID) and
   NOT EXISTS(select TechnologyID from tblApplicationVsTechnologies where TechnologyID = @TechnologyID) and
   NOT EXISTS(select TechnologyID from tblEffortHours where TechnologyID = @TechnologyID))
BEGIN
 DELETE from tblTechnologies where TechnologyID = @TechnologyID
 RETURN 1
END
   ELSE
     BEGIN
       Return 0
     END
END


Now in c# code, create onClick event for image button
C#
private void ImageButton_Click(object sender, EventArgs e)
{
    using (SqlConnection con = new SqlConnection("Your database connection string here."))
    {
        using (SqlCommand cmd = new SqlCommand("usp_DelTechnology", con))
        {
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.Add("@TechnologyID", SqlDbType.int).Value = GridView1.Cells[0].Text; //access technology Id from Gridview
           con.Open();
           int returnValue = cmd.ExecuteNonQuery();
           if (returnvalue == 0)
           {
               //Display message - There are references
           }
           else
           {
               //Deleted Successfully message.
           }

        }
    }
}
 
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