Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to correct a C# console application to execute a stored procedure?

Posted on 2012-04-09
2
Medium Priority
?
213 Views
Last Modified: 2012-06-21
I have written the following C# console appliation to execute a stored procedure but no output is generated. Can you tell me where I went wrong? I don't have a SQL table to write to at the present time.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Globalization;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data.Odbc;
using System.Runtime.InteropServices;

namespace ENA
{
    class Program
    {
       
        static OdbcConnection cn1 = new System.Data.Odbc.OdbcConnection
                ("DSN=1;UID=2;PWD=7;");

        static void Main(string[] args)
        {
            //   using (StreamReader sr = new StreamReader("NameAddr.txt"))
            cn1.Open();
            try
            {
                //     test
                string strAct = "";
                //string strLastName = "";
                strAct = "019172436";
                               
                ///////  call StoredProced //////
                OdbcCommand cmd = new OdbcCommand("getTATGATCDSAByAccount", cn1);
                                cmd.CommandType = CommandType.StoredProcedure;
             
                cmd.CommandText = "exec getTATGATCDSAByAccount @ApplnNm='test', @accountNumber='" + strAct + "'";
                cmd.CommandText = cmd.CommandText + ",@accountSystem=1435,@selectFields='accountNumber, accountStatus";
                cmd.CommandText = cmd.CommandText + ", fullName1, address1, city, stateCode, postalCode, taxID, accountClassification";
                cmd.CommandText = cmd.CommandText + ", fullName2, countryCode, countryOfResidence, countryOfCitizenship, accountCategory";
                cmd.CommandText = cmd.CommandText + ", cashIndicator, marginIndicator, codIndicator'";
               
                OdbcDataReader reader = cmd.ExecuteReader();
               
                Console.WriteLine();

            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message);
                return;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return;
            }
        }
    }
}
0
Comment
Question by:zimmer9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 2000 total points
ID: 37825820
You are specifying a stored procedure to call but then are specifying commandtext where you need to specify parameters see the linked articles odbc section for an example of how to do this :

http://msdn.microsoft.com/en-us/library/yy6y35y8(v=vs.71).aspx
0
 
LVL 10

Expert Comment

by:himanshut
ID: 37825841
Put a debugger at this line -->   cmd.CommandText = cmd.CommandText + ", cashIndicator, marginIndicator, codIndicator'";


and see what text do you have.

Copy that text and execute it in database itself to see if your stored procedure returns any values in SQL?
I think it would be more to do with filters and hence you are not getting results.

TEST IT BY:
reduce filters something like in stored procedure: just keep select * from tblname where id = @ID

and pass @ID from .net to SQL as parameter and see if you get any results there.

Cheers
0

Featured Post

Basic Security of Your VPC

So, you’ve got this shiny new VPC and a fancy new application configured on your EC2 servers ready to go. This application is only accessible from your computer, which is great for security, but you need your users to be able to access it! So, what’s the easiest way to do this?

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

704 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