Solved

Convert Command text to stored procedure in asp.net c#

Posted on 2010-11-27
4
633 Views
Last Modified: 2012-05-10
Hello,

I want the following code in asp.net c#  to be changed to use stored procedure with two parameters instead of command text.

 
conReport.Open();
            cmdReport.CommandType = CommandType.Text;
            cmdReport.Connection = conReport;
            cmdReport.CommandText = "Select  * FROM mytable";
            drReport = cmdReport.ExecuteReader();
            dsReport.Tables[0].Load(drReport);
            drReport.Close();
            conReport.Close();

Open in new window


Thanks

Zaki
0
Comment
Question by:zaki100
4 Comments
 
LVL 13

Accepted Solution

by:
Pryrates earned 250 total points
ID: 34221556
You can switch the CommandType to .StoredProcedure.
Then you can add your parameters.
 "Name" and "Type" must match your parameters of your stored procedure.

you can try something like shown in the code box

Hope that helps
cmdReport.CommandType = System.Data.CommandType.StoredProcedure;
SqlParameter param = new SqlParameter("Param1", System.Data.SqlDbType.Int);
param.Value = DBNull.Value;
cmdReport.Parameters.Add(param);

param = new SqlParameter("Param2", System.Data.SqlDbType.Int);
param.Value = DBNull.Value;
cmdReport.Parameters.Add(param);

drReport = cmdReport.ExecuteReader();

Open in new window

0
 
LVL 7

Assisted Solution

by:meispisces
meispisces earned 250 total points
ID: 34221787
conReport.Open();
                 cmdReport.Connection = conReport;
           cmdReport= new SqlCommand(StoredProcedureName, connection object)
            cmdReport.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.Add(Parameter-Name1, SqlDbType.Varchar).value= textbox1.text;
            drReport = cmdReport.ExecuteReader();
            dsReport.Tables[0].Load(drReport);
            drReport.Close();
            conReport.Close();

Change is in Bold. For more info refer http://www.c-sharpcorner.com/UploadFile/gtomar/storedprocedure12052007003126AM/storedprocedure.aspx

0
 
LVL 29

Expert Comment

by:Kumaraswamy R
ID: 34421770
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question