Hi all,
I'm trying to create a method Load that will call a stored procedure,the source code has some problems,really need some guidance still new in C#

Ok here's the code now with some minor adjustments rather than just getting one data,i'm planning on retrieving all the parameter just by getting the DateID,not sure if its the correct way...
The error is 'no overload for method data'
Since i'm new in C# can anyone suggest to me any good way to read error and fix it.

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()
        {
           
            SqlConnection conn = new SqlConnection("SOMEDATABASE");
            SqlCommand command = new SqlCommand("p_Date_sel", conn);
            SqlParameter parameter = new SqlParameter();
            parameter.ParameterName = "@DateID";
       

            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();

           }
01	public static void Main(string[] args)
02	        {
03	            /*No 56 is already in the table with all the parameters,
04	              i cant even show the dateID*/
05	            Program p = new Program(56);
06	            p.LoadData(56);
07	             
08	            Console.WriteLine("DateID = " + "" + p.dateID);
09	            Console.WriteLine("DateTypeID = " + "" + p.dateTypeID);
10	            Console.WriteLine("Date = " + "" + p.date);
11	            Console.WriteLine("Name = " + "" + p.name);
12	            Console.WriteLine("Notes = " + "" + p.notes);
13	             
14	            Console.ReadLine();
15	        }

Instead of trying to write the values from the reader to the Console, you need to assign them to their relevant variables (eg date, name, notes, etc).
Also, you are trying to pass a variable into the Load method - p.LoadData([U]56[/U]); - but the method does not have any input parameters.

Hi Ryshad

Ok here's what i've done,But i'm not sure about my Main program still blurry,do you mean that i'm passing a value that's already existed,it was suppose to get the dateID and retrive all the data from it.Below is my stored proc.The error is "Procedure or function 'p_Date_sel' expects parameter '@DateID', which was not supplied."

ALTER procedure [dbo].[p_Date_sel]
@DateID int

AS
BEGIN
SELECT DateTypeID , Date , Name, Notes <- do i need to insert dateid here as well?
FROM dbo.Dates
WHERE DateID= @DateID
END
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;


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

        public Program() 
        {
            _dateID = 0;
        }

        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 {return _dateID;} 
       }
       public int dateTypeID 
       {
           get { return _dateTypeID; }
           set { _dateTypeID  = value; } 
       }
       public DateTime date 
       {
           get {return _date ;}
           set { _date = value;} 
       }
       public string name 
       {
           get { return _name;}
           set { _name = value; } 
       }
       public string notes 
       {
           get { return _notes;}
           set { _notes = value; }
       }
      
        public void LoadData()
        {
            SqlConnection conn = new SqlConnection('somedatabase');
            conn.Open();
            SqlCommand command = new SqlCommand("p_Date_sel", conn);
            command.CommandType = CommandType.StoredProcedure;
            SqlDataReader reader = command.ExecuteReader();
            
            while (reader.Read()) 
            {
                _dateTypeID = Convert.ToInt16(reader["DateTypeID"]);
                _date = Convert.ToDateTime(reader["Date"]);
                _name = reader["Name"].ToString();
                _notes = reader["Notes"].ToString();
            }
            
           
            reader.Close();
            conn.Close();
            
           }
    }
}
namespace ExecuteStoredProcedure
{
    class TestClass 
    {
        public static void Main(string[] args)
        {
            
            Program p = new Program(5);
            p.LoadData();

            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();
        }
    }
}

Edited 6 Years Ago by BLY: edit database

Ok i've put the int dateID

public void LoadData(int dateID)

and change the

Console.WriteLine("DateID = " + "" + p.dateID.toString);

Is there something I'm missing?

This article has been dead for over six months. Start a new discussion instead.