Click here to Skip to main content
12,075,542 members (66,942 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"));
cmd.Connection.Open();
var dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
var dt = new DataTable();
dt.Load(dr);
Any help regarding this will be appreciated a lot. Thanks in advance!!
Posted 3-Mar-13 22:46pm
v2
Comments
sandeep207 4-Mar-13 3:52am
   
Hi

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 };
_conn.Open();
var dr = command.ExecuteReader(CommandBehavior.CloseConnection);
var dt = new DataTable();
dt.Load(dr);
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();
//dt.Load(dr);
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);
_conn.Open();
var command = new SqlCommand("LoadAppointment", _conn) { CommandType = commandType.StoredProcedure };

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

And, this is my stored Procedure

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

AS

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
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web04 | 2.8.160208.1 | Last Updated 4 Mar 2013
Copyright © CodeProject, 1999-2016
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