Go to DotNetFunda.com
 Online : 3091 |  Welcome, Guest!   Login
 
Home > Articles > ASP.NET > Working with Stored Procedures using asp.net
  • Nominate yourself for FREE online training by Microsoft MVP on OOPS, ASP.NET, ADO.NET and Sql Server.
    Brought to you by DotNetFunda.Com. You can refer to your friends as well !

  • Now you can recommend your article from any website to be selected as "Article of the Day" on DotNetFunda.Com website. If approved, that article will be featured on our home page.

General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.

Submit Article | Articles Home | Search Articles |

Working with Stored Procedures using asp.net

 Posted on: 9/9/2009 7:40:30 AM by G_arora | Views: 555 | Category: ASP.NET | Level: Beginner | Print Article
Stored procedure helps to make your work easy. With the help of these you just have to supply some parameters only.

Ask all your .NET related questions/clarifications here to get quicker solution.

Introduction
Stored procedure helps to make your work easy. With the help of these you just have to supply some parameters only. 

In the following code, I tried to explain the working with Stored procedure.
namespace CSharp.AStepAhead.usingstoredprocedure
{
    public class usingstoredprocedure
    {
        static string myConStr = "server=(local); integrated security=SSPI;database=HRnPAYROLL";
        static SqlConnection myCon;
        static SqlCommand myCmd;
        static SqlDataReader myDr;
        public static void Main()
        {
            int i = 0;
            string empId, fnm, lnm, age;

            for (; ; )
            {
                Console.Clear();

                Console.Write("\n 1 Insert Record(s)");
                Console.Write("\n 2 Update Record(s)");
                Console.Write("\n 3 Delete Record(s)");
                Console.Write("\n 4 Display Record(s)");
                Console.Write("\n 5 Display Record(s) By Employee ID");
                Console.Write("\n 6 Display Record(s) By Name Criteria");
                Console.Write("\n 7 Exit\n");
                Console.Write("\n Select your option [1-7]: ");
                i = int.Parse(Console.ReadLine());
                if (i <= 0)
                {
                    Console.WriteLine("\a Invalid Selection!");
                    Console.ReadLine();
                    continue;
                }
                if (i < 7)
                {
                    switch (i)
                    {
                        case 1: //Insert record
                            {
                                Console.Write(" Enter Employee Id:");
                                empId = Console.ReadLine();
                                Console.Write(" Enter First Name:");
                                fnm = Console.ReadLine();
                                Console.Write(" Enter Last Name:");
                                lnm = Console.ReadLine();
                                Console.Write(" Enter Age:");
                                age = Console.ReadLine();
                                Console.WriteLine("\n " + usingstoredprocedure.InsertEmployee(empId, fnm, lnm, age));
                                Console.ReadLine();
                                break;
                            }

                        case 2:
                            {
                                Console.Write(" Enter Employee Id:");
                                empId = Console.ReadLine();
                                Console.Write(" Enter First Name:");
                                fnm = Console.ReadLine();
                                Console.Write(" Enter Last Name:");
                                lnm = Console.ReadLine();
                                Console.Write(" Enter Age:");
                                age = Console.ReadLine();
                                Console.WriteLine("\n " + usingstoredprocedure.UpdateEmployee(empId, fnm, lnm, age));
                                Console.ReadLine();
                                break;
                            }

                        case 3:
                            {
                                Console.Write(" Enter Employee Id:");
                                empId = Console.ReadLine();
                                Console.WriteLine("\n " + usingstoredprocedure.DeleteEmployee(empId));
                                Console.ReadLine();
                                break;

                            }

                        case 4:
                            {
                                fnm = "";
                                empId = "";
                                usingstoredprocedure.dispData(fnm, empId);
                                break;
                            }

                        case 5:
                            {
                                Console.Write(" Enter Employee Id:");
                                empId = Console.ReadLine();
                                fnm = "";

                                usingstoredprocedure.dispData(fnm, empId);
                                break;
                            }

                        case 6:
                            {
                                Console.Write(" Enter Name:");
                                fnm = Console.ReadLine();
                                empId = "";
                                usingstoredprocedure.dispData(fnm, empId);
                                break;
                            }
                    }
                }
                if (i == 7)
                {
                    Console.Write("\n You are existing!");
                    break;
                }

            }
            Console.ReadLine();
        
Conclusion

Its easy to work with Stored Procedure.



Interesting?   Share and Bookmark this kick it on DotNetKicks.com


About Gaurav Arora

Experience:7 year(s)
Home page:http://www.msdotnetheaven.com/forums
Member since:Monday, September 07, 2009
Level:Starter
Status: [Member]
Biography:MCA, M.Phill(Comp.Sc.),MCP, MCTS, Sr. Software Engineer.
 Latest post(s) from G_arora

   ◘ Discussing Constructors in C# posted on 9/15/2009 5:58:48 AM
   ◘ Explore State Management posted on 9/11/2009 10:38:15 AM
   ◘ Working with Silverlight2 controls : A detailed look posted on 9/9/2009 9:31:20 AM
   ◘ Silverlight2 : A detailed look posted on 9/9/2009 9:04:17 AM
   ◘ Lets Start devlopment using Silverlight2 posted on 9/9/2009 8:04:09 AM


 Responses
Posted by: Raja | Posted on: 09 Sep 2009 11:45:26 AM

Hi Gaurav,

I liked your other article but this article is really tough to understand. Most of this article has code snippets and I couldn't understand how this will work for stored procedure like where usingstoredprocedure.InsertEmployee method?

Could you please explain this?

Thanks

Posted by: G_arora | Posted on: 10 Sep 2009 04:15:29 AM

Hello Raja,

Thanks for your appreciation. Need to upload attachment and full code. I will update the article with description within couple of days.

Regards,
Gaurav

Submit Article


About Us | The Team | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)