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 Los151.8K
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 Marcin Kozub 275
1 OriginalGriff 264
2 Sergey Alexandrovich Kryukov 215
3 Praneet Nadkar 197
4 Richard MacCutchan 182
0 OriginalGriff 8,048
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


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