Click here to Skip to main content
11,921,889 members (59,682 online)
Rate this:
Please Sign up or sign in to vote.
I am working with C# and MSSQL 2005 database.

I have this code, but my DataTable is not getting loaded with any data.

var conn = new SqlConnection(ConnectionString);
var cmd = new SqlCommand
                    Connection = conn,
                    CommandText = "LoadAppointment",
                    CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(new SqlParameter("Parametr Name", "Parameter value"));
var dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
var dt = new DataTable();
Any help regarding this will be appreciated a lot. Thanks in advance!!
Posted 3-Mar-13 22:46pm
sandeep207 4-Mar-13 3:52am

Please put all the inside try catach block and track send error message or send me code whta you have written
aspnet_regiis -i 4-Mar-13 3:52am
Of course not.. Because the code is commented
ArunAmalraj 4-Mar-13 3:55am
I just wanted to know why it does not works when it is uncommented too.
ArunAmalraj 4-Mar-13 3:53am
var command = new SqlCommand("LoadAppointment", _conn) { CommandType = CommandType.StoredProcedure };
var dr = command.ExecuteReader(CommandBehavior.CloseConnection);
var dt = new DataTable();
sandeep207 4-Mar-13 3:54am
do not use var //dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); instead use
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
DataTable dt = new DataTable();
ArunAmalraj 4-Mar-13 3:59am
Tried it as per ur advice. But it doesn't made any difference.

Even tried of Building the solution again.
Mycroft Holmes 4-Mar-13 3:55am
Presumably your parameter name is correctly formatted, your parameter value is a variable and is populated and passed in correctly and presumably you have debugged the code to check the procedure is called correctly and you have tested the procedure in SSMS with the parameter passed in.
ArunAmalraj 4-Mar-13 3:56am
I dont have any Parameters for the Stored Procedure i am using.
Mycroft Holmes 4-Mar-13 4:10am
WAT then why are you creating a Parameter and adding it to the command. This MUST generate a sql error.
ArunAmalraj 4-Mar-13 4:15am
I really dont get what you mean.

This is my code

_conn = new SqlConnection(ConnectionString);
var command = new SqlCommand("LoadAppointment", _conn) { CommandType = commandType.StoredProcedure };

SqlDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection);
DataTable dt = new DataTable();

And, this is my stored Procedure

USE [Scheduler]
/****** Object: StoredProcedure [dbo].[LoadAppointment] Script Date: 03/04/2013 14:44:28 ******/
ALTER PROCEDURE [dbo].[LoadAppointment]


SELECT * FROM [Scheduler].[dbo].[tblAppointments]

Note: My Stored Procedure Name is passed as a parameter associated with the Connection String.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1


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

  Print Answers RSS
0 OriginalGriff 568
1 Dave Kreskowiak 285
2 Sergey Alexandrovich Kryukov 173
3 Richard MacCutchan 155
4 ppolymorphe 135
0 OriginalGriff 7,036
1 KrunalRohit 4,530
2 Sergey Alexandrovich Kryukov 3,286
3 George Jonsson 2,840
4 Suvendu Shekhar Giri 2,216

Advertise | Privacy | Mobile
Web04 | 2.8.151120.1 | Last Updated 4 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100