Click here to Skip to main content
Rate this: bad
good
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"));
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 at 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 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 };
_conn.Open();
var dr = command.ExecuteReader(CommandBehavior.CloseConnection);
var dt = new DataTable();
dt.Load(dr);
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();
//dt.Load(dr);
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);
_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
0 OriginalGriff 195
1 Afzaal Ahmad Zeeshan 152
2 ProgramFOX 130
3 Maciej Los 130
4 den2k88 80
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,657
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 4 Mar 2013
Copyright © CodeProject, 1999-2014
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