Solved

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

Posted on 2010-11-27
4
629 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now