Click here to Skip to main content
11,709,529 members (44,303 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: SQL-server-2005 C#
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 21:46pm
sandeep207 at 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 at 4-Mar-13 3:52am
Of course not.. Because the code is commented
ArunAmalraj at 4-Mar-13 3:55am
I just wanted to know why it does not works when it is uncommented too.
ArunAmalraj at 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 at 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 at 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 at 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 at 4-Mar-13 3:56am
I dont have any Parameters for the Stored Procedure i am using.
Mycroft Holmes at 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 at 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 Sergey Alexandrovich Kryukov 590
1 OriginalGriff 250
2 Maciej Los 225
3 Peter Leow 219
4 Mika Wendelius 160
0 OriginalGriff 9,428
1 Sergey Alexandrovich Kryukov 9,027
2 CPallini 5,189
3 Maciej Los 4,991
4 Mika Wendelius 3,856

Advertise | Privacy | Mobile
Web03 | 2.8.150819.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