Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all
Below is my code to get data from a stored procedure but i keep getting error on this line

SqlDataReader reader = command.ExecuteReader();

<pre>
view source
print?
01	using System;
02	using System.Collections.Generic;
03	using System.Data;
04	using System.Data.SqlClient;
05	using System.Configuration;
06	using System.Text;
07	 
08	 
09	namespace ExecuteStoredProcedure
10	{
11	    class Program
12	    {
13	        private int _dateID;
14	 
15	        public Program(int dateID) {
16	            _dateID = dateID;
17	        }
18	 
19	       public int dateID { get; set; }
20	 
21	        public void LoadData(int p)
22	        {
23	            
24	            SqlConnection conn = new SqlConnection("SOMEDATABASE");
25	            SqlCommand command = new SqlCommand("p_Date_sel", conn);
26	            SqlParameter parameter = new SqlParameter();
27	            parameter.ParameterName = "@DateID";
28	             
29	            command.CommandType = CommandType.StoredProcedure;
30	            command.Parameters.Add(parameter);
31	 
32	            conn.Open();
33	            SqlDataReader reader = command.ExecuteReader();
34	            while (reader.Read())
35	            {
36	                Console.WriteLine(reader["Dates"]);
37	            }
38	            conn.Close();
39	 
40	           }
41	    }
42	}


Here's a main class if your intrested

view source
01	using System;
02	using System.Collections.Generic;
03	using System.Linq;
04	using System.Text;
05	 
06	namespace ExecuteStoredProcedure
07	{
08	    class TestClass
09	    {
10	        public static void Main(string[] args)
11	        {
12	            Program p = new Program(56);
13	            p.LoadData(56);
14	            Console.WriteLine("DateID = " + "" + p.dateID);
15	            Console.ReadLine();
16	        }
17	    }
18	}


Please tell me what i'm doing wrong.I'm a new developer still under training, the next step for me would be creating a insert,update and delete method.If your willing to teach i'm willing to learn,thanks in advance
Posted
Comments
Arun Jacob 17-Aug-10 1:37am    
Whats the error?

 
Share this answer
 
I can't find code where you set the value of the parameter in the
public void LoadData(int p)


SqlConnection conn = new SqlConnection("SOMEDATABASE");
SqlCommand command = new SqlCommand("p_Date_sel", conn);
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "@DateID";


//You are missing following line.
<big>parameter.Value = p;</big>

command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(parameter);

conn.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
    Console.WriteLine(reader["Dates"]);
}
conn.Close();
 
Share this answer
 
v2
Hi All,

Thank you for helping.
Now i'm getting an error on dates,is this the correct way for me to do this,Planning on getting all the parameters out just from the dateID

class Program
    {
        private int _dateID;
        private int _dateTypeID;
        private DateTime _date;
        private string _name;
        private string _notes;


        public Program(int dateID) {
            _dateID = dateID;
        }
        public Program(int datetypeID,DateTime date,string name,string notes){
        
            _dateTypeID = datetypeID;
            _date = date;
            _name = name;
            _notes = notes;
        }

        

       public int dateID { get; set; }
       public int dateTypeID { get; set; }
       public DateTime date { get; set; }
       public string name { get; set; }
       public string notes { get; set; }
       //suppose to retrieve all the data from DateID and show all
       // the attributes in table Date
       //Am i doing this the right way?


        
        public void LoadData(int p)
        {
           
            SqlConnection conn = new SqlConnection("Data Source=mycbj01psql03\\sandbox01;Initial Catalog=DBRMS;Integrated Security=True");
            SqlCommand command = new SqlCommand("p_Date_sel", conn);
            SqlParameter parameter = new SqlParameter();
            parameter.ParameterName = "@DateID";
            parameter.Value = p;// just added this 

            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "p_Date_sel";
            
            command.Parameters.Add(parameter);


            conn.Open();
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                Console.WriteLine(reader["Dates"]); //error on this line 
            }
            conn.Close();

           }
    }


public static void Main(string[] args)
        {
            /*No 56 is already in the table with all the parameters,
              i cant even show the dateID*/
            Program p = new Program(56); 
            p.LoadData(56);
            
            Console.WriteLine("DateID = " + "" + p.dateID);
            Console.WriteLine("DateTypeID = " + "" + p.dateTypeID);
            Console.WriteLine("Date = " + "" + p.date);
            Console.WriteLine("Name = " + "" + p.name);
            Console.WriteLine("Notes = " + "" + p.notes);
            
            Console.ReadLine();
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900