Click here to Skip to main content
15,887,347 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi Everyone...
I am going to create a global function in c# that will execute my Stored Procedure. When ever i will call to this function it will execute my procedure.

Bellow i have created a function it will give me correct results but this function will show me only that procedure result which parameters will match to it. i want this function to be global.

What I have tried:

My Function is bellow
C#
public static void exec_proc(string proc,string param)
   {
       SqlCommand cmd = new SqlCommand(proc,DBConnection());
       cmd.CommandType = CommandType.StoredProcedure;
       cmd.Parameters.Add("@DESCRIPTION", SqlDbType.VarChar,30).Value = param;
       cmd.ExecuteNonQuery();
   }



i have call this function in my asp.net page as follows it works for me
db.exec_proc("dbo.CREATE_CATAGORY", txt_Cat_Name.Text);
Posted
Updated 26-Dec-16 0:42am
Comments
F-ES Sitecore 20-Dec-16 4:25am    
Your question doesn't make much sense. If you want to call a different procedure that doesn't need parameters then simply update your code to do that? It's unclear exactly what it is you are asking.
Muhammd Aamir 20-Dec-16 4:34am    
Ok Thankx F-ES just like above the function will accept 2 param let suppose i have to call this function where i have 3 params what i will have to do ???
F-ES Sitecore 20-Dec-16 4:44am    
public static void exec_proc(string proc,string param, string param2)
{
SqlCommand cmd = new SqlCommand(proc,DBConnection());
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@DESCRIPTION", SqlDbType.VarChar,30).Value = param;
cmd.Parameters.Add("@MyOtherParam", SqlDbType.VarChar,30).Value = param2;
cmd.ExecuteNonQuery();
}
Muhammd Aamir 20-Dec-16 4:57am    
F-ES Sitecore Thankx For sharing your knowledge with me but the problem still same... Kindly show me a function that will do the same without creating new function
F-ES Sitecore 20-Dec-16 5:00am    
public static void exec_proc(string proc,string param, string param2 = null)
{
if (string.IsNullOrWhiteSpace(param2))
{
// param2 was not supplied
}
else
{
// param2 was supplied
}
}

1 solution

if number of parameter is fixed then you can use following way for dynamically call

public static void exec_proc(string proc,string param,string parametetName, DbType parameterType,int pamaterSize)
{
SqlCommand cmd = new SqlCommand(proc,DBConnection());
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(parametetName, parameterType,pamaterSize).Value = param;
cmd.ExecuteNonQuery();
}
 
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