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#
Hello,
 
Below is my Stored Procedure and Code.
 
Using this i would like to load the values in the database to my Form Controls "Datasource" property.
 
But i could not be able to retrieve any values.
 
My Code:
 
_conn = new SqlConnection(ConnectionString);
            var command = new SqlCommand("LoadAppointment", _conn) { CommandType = CommandType.StoredProcedure };
            _conn.Open();
            var dr = command.ExecuteReader(CommandBehavior.CloseConnection);
            var dt = new DataTable();
            dt.Load(dr);
 

 

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: I am not supposed to use a Dataset.
Posted 3-Mar-13 23:31pm
Edited 3-Mar-13 23:36pm
Maciej Los158.9K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You need to provide the schema for data table.
In you code you have simply created the table - but that does not have any columns added to that.
 
Please go through this link - It will help you achieving this.
 
DataTable.Load Method
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I have used SqlDataReader instead of using SqlDataAdapter or DataSet.
 
DataTable dtTable = new DataTable();
            SqlConnection sqlCon = new SqlConnection(@"Server=WILLINGTON\SQLEXPRESS;Database=TestDB;Trusted_Connection=True;");
            SqlCommand sqlCmd = new SqlCommand("SELECT * FROM USERS", sqlCon);
 
            if (sqlCon.State != ConnectionState.Open)
                sqlCon.Open();
 
            SqlDataReader dr = sqlCmd.ExecuteReader();
            dtTable.Load(dr);
 
            if (sqlCon.State != ConnectionState.Open)
                sqlCon.Close();
  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 280
1 Jochen Arndt 150
2 Kornfeld Eliyahu Peter 120
3 PIEBALDconsult 110
4 Richard MacCutchan 105
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web01 | 2.8.141220.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