SqlDataReader result to List [duplicate]










0















This question already has an answer here:



  • What is a NullReferenceException, and how do I fix it?

    32 answers



I am trying to pull records from sql server by using SqlDataReader and dump to a List. I am having object reference not set to instance of an object error. Below is the code.



 private List<Models.AreasOfLaw> aolTitles;

public List<Models.AreasOfLaw> AreasOfLawListItems

get return aolTitles;
set aolTitles = value; OnPropertyChanged("AoLTitles");


private void GetAllAreasOfLaws()

try

using (SqlConnection sqlConnection = new SqlConnection(DataSources.RemoteConnectionString()))

sqlConnection.Open();
string commandText = "SELECT AreaOfLawTitle FROM [dbo].[CLR.AreaOfLaw] ORDER BY AreaOfLawTitle";
using (SqlCommand sqlCommand = new SqlCommand(commandText, sqlConnection))

sqlCommand.CommandType = CommandType.Text;
SqlDataReader reader = sqlCommand.ExecuteReader();
while (reader.Read())

string aolTitle = Convert.ToString(reader["AreaOfLawTitle"]);


AreasOfLawListItems.Add(new Models.AreasOfLaw AreaOfLawTitle = aolTitle );

int n = AreasOfLawListItems.Count;

sqlConnection.Close();



catch (Exception ex)






I am desperate folks. Thank you.










share|improve this question













marked as duplicate by Tetsuya Yamamoto, mjwills, Richard c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 12 at 9:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • a) Use Dapper. b) Which line of code throws the exception?
    – mjwills
    Nov 12 at 9:12










  • Use the breakpoints int he beginning of the function to see what line throws the exception.
    – HEWhoDoesn'tKnow
    Nov 12 at 9:12






  • 1




    You forgot to initialise AreasOfLawListItems / aolTitles.
    – mjwills
    Nov 12 at 9:13










  • the 'AreasOfLawListItems.Add(new Models.AreasOfLaw AreaOfLawTitile = aolTitle);
    – Bainn
    Nov 12 at 9:15










  • @Bainn you did not initialize your list
    – HEWhoDoesn'tKnow
    Nov 12 at 9:16















0















This question already has an answer here:



  • What is a NullReferenceException, and how do I fix it?

    32 answers



I am trying to pull records from sql server by using SqlDataReader and dump to a List. I am having object reference not set to instance of an object error. Below is the code.



 private List<Models.AreasOfLaw> aolTitles;

public List<Models.AreasOfLaw> AreasOfLawListItems

get return aolTitles;
set aolTitles = value; OnPropertyChanged("AoLTitles");


private void GetAllAreasOfLaws()

try

using (SqlConnection sqlConnection = new SqlConnection(DataSources.RemoteConnectionString()))

sqlConnection.Open();
string commandText = "SELECT AreaOfLawTitle FROM [dbo].[CLR.AreaOfLaw] ORDER BY AreaOfLawTitle";
using (SqlCommand sqlCommand = new SqlCommand(commandText, sqlConnection))

sqlCommand.CommandType = CommandType.Text;
SqlDataReader reader = sqlCommand.ExecuteReader();
while (reader.Read())

string aolTitle = Convert.ToString(reader["AreaOfLawTitle"]);


AreasOfLawListItems.Add(new Models.AreasOfLaw AreaOfLawTitle = aolTitle );

int n = AreasOfLawListItems.Count;

sqlConnection.Close();



catch (Exception ex)






I am desperate folks. Thank you.










share|improve this question













marked as duplicate by Tetsuya Yamamoto, mjwills, Richard c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 12 at 9:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • a) Use Dapper. b) Which line of code throws the exception?
    – mjwills
    Nov 12 at 9:12










  • Use the breakpoints int he beginning of the function to see what line throws the exception.
    – HEWhoDoesn'tKnow
    Nov 12 at 9:12






  • 1




    You forgot to initialise AreasOfLawListItems / aolTitles.
    – mjwills
    Nov 12 at 9:13










  • the 'AreasOfLawListItems.Add(new Models.AreasOfLaw AreaOfLawTitile = aolTitle);
    – Bainn
    Nov 12 at 9:15










  • @Bainn you did not initialize your list
    – HEWhoDoesn'tKnow
    Nov 12 at 9:16













0












0








0








This question already has an answer here:



  • What is a NullReferenceException, and how do I fix it?

    32 answers



I am trying to pull records from sql server by using SqlDataReader and dump to a List. I am having object reference not set to instance of an object error. Below is the code.



 private List<Models.AreasOfLaw> aolTitles;

public List<Models.AreasOfLaw> AreasOfLawListItems

get return aolTitles;
set aolTitles = value; OnPropertyChanged("AoLTitles");


private void GetAllAreasOfLaws()

try

using (SqlConnection sqlConnection = new SqlConnection(DataSources.RemoteConnectionString()))

sqlConnection.Open();
string commandText = "SELECT AreaOfLawTitle FROM [dbo].[CLR.AreaOfLaw] ORDER BY AreaOfLawTitle";
using (SqlCommand sqlCommand = new SqlCommand(commandText, sqlConnection))

sqlCommand.CommandType = CommandType.Text;
SqlDataReader reader = sqlCommand.ExecuteReader();
while (reader.Read())

string aolTitle = Convert.ToString(reader["AreaOfLawTitle"]);


AreasOfLawListItems.Add(new Models.AreasOfLaw AreaOfLawTitle = aolTitle );

int n = AreasOfLawListItems.Count;

sqlConnection.Close();



catch (Exception ex)






I am desperate folks. Thank you.










share|improve this question














This question already has an answer here:



  • What is a NullReferenceException, and how do I fix it?

    32 answers



I am trying to pull records from sql server by using SqlDataReader and dump to a List. I am having object reference not set to instance of an object error. Below is the code.



 private List<Models.AreasOfLaw> aolTitles;

public List<Models.AreasOfLaw> AreasOfLawListItems

get return aolTitles;
set aolTitles = value; OnPropertyChanged("AoLTitles");


private void GetAllAreasOfLaws()

try

using (SqlConnection sqlConnection = new SqlConnection(DataSources.RemoteConnectionString()))

sqlConnection.Open();
string commandText = "SELECT AreaOfLawTitle FROM [dbo].[CLR.AreaOfLaw] ORDER BY AreaOfLawTitle";
using (SqlCommand sqlCommand = new SqlCommand(commandText, sqlConnection))

sqlCommand.CommandType = CommandType.Text;
SqlDataReader reader = sqlCommand.ExecuteReader();
while (reader.Read())

string aolTitle = Convert.ToString(reader["AreaOfLawTitle"]);


AreasOfLawListItems.Add(new Models.AreasOfLaw AreaOfLawTitle = aolTitle );

int n = AreasOfLawListItems.Count;

sqlConnection.Close();



catch (Exception ex)






I am desperate folks. Thank you.





This question already has an answer here:



  • What is a NullReferenceException, and how do I fix it?

    32 answers







c# list sqlclient






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 9:11









Bainn

146




146




marked as duplicate by Tetsuya Yamamoto, mjwills, Richard c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 12 at 9:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Tetsuya Yamamoto, mjwills, Richard c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 12 at 9:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • a) Use Dapper. b) Which line of code throws the exception?
    – mjwills
    Nov 12 at 9:12










  • Use the breakpoints int he beginning of the function to see what line throws the exception.
    – HEWhoDoesn'tKnow
    Nov 12 at 9:12






  • 1




    You forgot to initialise AreasOfLawListItems / aolTitles.
    – mjwills
    Nov 12 at 9:13










  • the 'AreasOfLawListItems.Add(new Models.AreasOfLaw AreaOfLawTitile = aolTitle);
    – Bainn
    Nov 12 at 9:15










  • @Bainn you did not initialize your list
    – HEWhoDoesn'tKnow
    Nov 12 at 9:16
















  • a) Use Dapper. b) Which line of code throws the exception?
    – mjwills
    Nov 12 at 9:12










  • Use the breakpoints int he beginning of the function to see what line throws the exception.
    – HEWhoDoesn'tKnow
    Nov 12 at 9:12






  • 1




    You forgot to initialise AreasOfLawListItems / aolTitles.
    – mjwills
    Nov 12 at 9:13










  • the 'AreasOfLawListItems.Add(new Models.AreasOfLaw AreaOfLawTitile = aolTitle);
    – Bainn
    Nov 12 at 9:15










  • @Bainn you did not initialize your list
    – HEWhoDoesn'tKnow
    Nov 12 at 9:16















a) Use Dapper. b) Which line of code throws the exception?
– mjwills
Nov 12 at 9:12




a) Use Dapper. b) Which line of code throws the exception?
– mjwills
Nov 12 at 9:12












Use the breakpoints int he beginning of the function to see what line throws the exception.
– HEWhoDoesn'tKnow
Nov 12 at 9:12




Use the breakpoints int he beginning of the function to see what line throws the exception.
– HEWhoDoesn'tKnow
Nov 12 at 9:12




1




1




You forgot to initialise AreasOfLawListItems / aolTitles.
– mjwills
Nov 12 at 9:13




You forgot to initialise AreasOfLawListItems / aolTitles.
– mjwills
Nov 12 at 9:13












the 'AreasOfLawListItems.Add(new Models.AreasOfLaw AreaOfLawTitile = aolTitle);
– Bainn
Nov 12 at 9:15




the 'AreasOfLawListItems.Add(new Models.AreasOfLaw AreaOfLawTitile = aolTitle);
– Bainn
Nov 12 at 9:15












@Bainn you did not initialize your list
– HEWhoDoesn'tKnow
Nov 12 at 9:16




@Bainn you did not initialize your list
– HEWhoDoesn'tKnow
Nov 12 at 9:16












1 Answer
1






active

oldest

votes


















1














Your list aolTItles is never initiliased.



Your code should look like this



private List<Models.AreasOfLaw> aolTitles = new List<Models.AreasOfLaw>();

public List<Models.AreasOfLaw> AreasOfLawListItems

get return aolTitles;
set aolTitles = value; OnPropertyChanged("AoLTitles");



Declaring a variable and intiliasing it are 2 separate steps, and you can't use a variable until you've initialised it.






share|improve this answer


















  • 1




    Thanks for the reminder. It worked. Thank you very much indeed.
    – Bainn
    Nov 12 at 9:28

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Your list aolTItles is never initiliased.



Your code should look like this



private List<Models.AreasOfLaw> aolTitles = new List<Models.AreasOfLaw>();

public List<Models.AreasOfLaw> AreasOfLawListItems

get return aolTitles;
set aolTitles = value; OnPropertyChanged("AoLTitles");



Declaring a variable and intiliasing it are 2 separate steps, and you can't use a variable until you've initialised it.






share|improve this answer


















  • 1




    Thanks for the reminder. It worked. Thank you very much indeed.
    – Bainn
    Nov 12 at 9:28















1














Your list aolTItles is never initiliased.



Your code should look like this



private List<Models.AreasOfLaw> aolTitles = new List<Models.AreasOfLaw>();

public List<Models.AreasOfLaw> AreasOfLawListItems

get return aolTitles;
set aolTitles = value; OnPropertyChanged("AoLTitles");



Declaring a variable and intiliasing it are 2 separate steps, and you can't use a variable until you've initialised it.






share|improve this answer


















  • 1




    Thanks for the reminder. It worked. Thank you very much indeed.
    – Bainn
    Nov 12 at 9:28













1












1








1






Your list aolTItles is never initiliased.



Your code should look like this



private List<Models.AreasOfLaw> aolTitles = new List<Models.AreasOfLaw>();

public List<Models.AreasOfLaw> AreasOfLawListItems

get return aolTitles;
set aolTitles = value; OnPropertyChanged("AoLTitles");



Declaring a variable and intiliasing it are 2 separate steps, and you can't use a variable until you've initialised it.






share|improve this answer














Your list aolTItles is never initiliased.



Your code should look like this



private List<Models.AreasOfLaw> aolTitles = new List<Models.AreasOfLaw>();

public List<Models.AreasOfLaw> AreasOfLawListItems

get return aolTitles;
set aolTitles = value; OnPropertyChanged("AoLTitles");



Declaring a variable and intiliasing it are 2 separate steps, and you can't use a variable until you've initialised it.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 9:27









WhatsThePoint

2,15342036




2,15342036










answered Nov 12 at 9:16









Dave

1,4941720




1,4941720







  • 1




    Thanks for the reminder. It worked. Thank you very much indeed.
    – Bainn
    Nov 12 at 9:28












  • 1




    Thanks for the reminder. It worked. Thank you very much indeed.
    – Bainn
    Nov 12 at 9:28







1




1




Thanks for the reminder. It worked. Thank you very much indeed.
– Bainn
Nov 12 at 9:28




Thanks for the reminder. It worked. Thank you very much indeed.
– Bainn
Nov 12 at 9:28



這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto