Save dataset value as integer

I have a dataset that has only one column and one row. This gives me an integer. I have to save it in another whole. I am a student. please help me! Thanks!

Dataset pds; if (pds.Tables[0].Rows.Count > 0) { int Islocationcount = pds.Tables[0].Columns[0].ColumnName[1]; } 
+4
source share
5 answers

Do you want to

 int Islocationcount = Convert.ToInt32(pds.Tables[0].Rows[1][0]); 

Assuming this will not be DBNull.Value , otherwise it will throw an exception

+3
source

.NET has some features that may help. Departure:

System.Convert

int.TryParse

Also, make sure you check that the value exiting the column is DBNull.Value, which is a common source of runtime errors for new .NET developers doing this.

0
source

int value = (int) pds.Tables [0] .Rows [0] ["ColumnName"]; // or can use column index 0

0
source

You can check here a few things with such things. I usually check that Dataset not null, and then make sure it has at least one table and one row. If he has all these things, then double-check that the value you are looking for is not null and is an actual integer. Here is an example (VB.NET because I am familiar):

 Dim IsLocationCounter As Integer If (Not pds Is Nothing AndAlso pds.Tables.Count > 0 AndAlso pds.Tables[0].Rows.Count > 0) Then /* I can't remember here if you can use <> or if you have to use Is */ If (pds.Tables[0].Columns[0].ColumnName[1] <> DBValue.Null) Then /* Because you pass an integer by reference to TryParse, you don't have to set anything in an else statement */ If ( Not Integer.TryParse(pds.Tables[0].Columns[0].ColumnName[1].ToString(), IsLocationcounter) ) Then Throw New Exception ("Do some error handling here because there is no int coming back in your dataset") End If End If End If 

As a note for the example that you have in your question, you cannot use this IsLocationCount variable outside this If statement if you declare it inside an If statement. If you need it outside the If statement, you must declare it outside of the If statement.

0
source

Dim value As integer = ds.Tables (0) .Rows (0) (0)

0
source

All Articles