Solved

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

17 Experts available now in Live!

Get 1:1 Help Now