Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

.ToString - Simple question 1

Status
Not open for further replies.

Corinne

Programmer
May 15, 2001
146
US
Hi,

I'm still very new to .NET but have worked w/vb6 for a while. I'm having a problem understanding the .ToString method. I have my connection, dataadapter & dataset working ok. I basically just want to wriet a query to give ne results from the dataset. In that result set I want to test for the value of 1 field. In the past I did a query, populated a recordset & then filled a string with that recordset value & then went on my merry way. For some reason I'm having an issue with applying that concept in .NET. Can someone please give me the basic 101 version of how to work with .ToString? (I'm assuming that is what I need to use.)

Thanks,
Corinne
 
The .ToString converts a value into a string, like CStr in VB6 would do.

So you would something like this:

Dim myString as String

myString = Convert.ToString(your dataset value here)

HTH
 
Cenedra,
I'm still having a mental block on this. I included the portion of my code where I'm trying to populate the strAccess with the PhoneUserValue returned by the query:

Dim selectCMD As SqlCommand = New SqlCommand("SELECT LANID, PhoneUser, PhoneUserValue FROM ztblAdmin WHERE LANID = '" & strEmployeeID & "'", SqlConnection1)SelectCMD.CommandTimeout = 30

Dim DA_Admin1 As SqlDataAdapter = New SqlDataAdapter()
DA_Admin1.SelectCommand = selectCMD

Dim DS_Admin1 As DataSet = New DataSet()
DA_Admin1.Fill(DS_Admin1, "PhoneUserValue")

Dim strAccess As String
strAccess = Convert.ToString(DS_Admin1)


Where am I missing the point??

Thanks,
Corinne
 
First of all, I have a question -

are you expecting a single record returned or multiple records?

The problem is here: strAccess = Convert.ToString(DS_Admin1)


You are trying to convert an entire dataset into a string. You have to reference the individual field you are placing into that string.
 
The query will return only one record and I want to place the value for a specific field into the string value. I tried the line:
strAccess = Convert.ToString(DS_Admin1, "PhoneUserValue") and this produces a build error: "Overload resolution failed because no accessible 'ToString' can be called with these arguments. And tehn all of teh arguments are listed.

Thanks,
Corinne
 
Corinne,

A DataSet is an object which contains one or more DataTables.

A DataTable is an object that contains one or more DataRows and columns.

You need to access the DataRow.

Try something like:
Code:
yourValue = DataSet1.Tables(0).Rows(0).Item(0).

The index of item being the column you want.

On the other hand, since you are using SQL Server, and returning a very small amount of data, I would suggest using a stored procedure and output parameters.

When I want one row, comprised of a few columns returned, I usually execute a stored procedure from a SqlCommand object, and look at the output parameter values rather than fill a DataSet.


 
Thanks for the thorough answer RiverGuy, a star for you :)

I would have gone into more detail if I hadn't been called away to a meeting :)
 
RiverGuy,

Could you please go into your response a little further. Right now I'm becomming very confused over something that should be extremely simple. I'd like to try to accomplish this string population with the example I have already started. I'll try the stored procedure next time. I placed the line:

strAccess = DS_Admin1.Tables(0).Rows(0).Item(0) and received an error:
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in system.data.dll Additional information: There is no row at position 0.

Does this mean that I am not populating the dataset corredctly and it is not finding any data? How can I find that out? Which brings me to another question that I should probably start another thread for but here goes anyways: How do I test for values when I an running? In VB I always used the immediate window. I can't seem to find a way to test like that in .NET.

Corinne
 
Yes, it means that the DataTable you are looking at has no Rows. You can use the IsDBNull function to see if it is a null value.

Remember, not every object has a .ToString() method. You would have to concatenate the different cells you are retreiving from your DataTable together to make a string.
 
Like I said before, I'm completely confused. Can you possibily point out where I have gone wrong?

I have one table that I am trying to query and bring back a result set which is now called a dataset.

All I would like to do is then place the value of a specific field in that dataset into a string so that I can then test against it. How in .NET can I do that?????? As I posted before I would like to do this without using a stored procedure at this time as I'm so confused now I don't really add another item to my learning curve today.

Code so far:
Dim selectCMD As SqlCommand = New SqlCommand("SELECT LANID, PhoneUser, PhoneUserValue FROM ztblAdmin WHERE LANID = '" & strEmployeeID & "'", SqlConnection1)SelectCMD.CommandTimeout = 30

Dim DA_Admin1 As SqlDataAdapter = New SqlDataAdapter()
DA_Admin1.SelectCommand = selectCMD

Dim DS_Admin1 As DataSet = New DataSet()
DA_Admin1.Fill(DS_Admin1, "PhoneUserValue")

Dim strAccess As String
strAccess = DS_Admin1.Tables(0).Rows(0).Item(0)

Can anyone see where I have gone wrong so far? How exactally would I use the IsDBNull function in this instance? An example(so simple that the completely lost (me) could follow) would be greatly appreciated at this point.

Thanks
 
You've named your DataTable. Try referencing it in the index:

Code:
strAccess = DS_Admin1.Tables("PhoneUserValue").Rows(0).Item(0)

To me, that seems odd. Is PhoneUserValue really the name of your table?

Anyhow, you can also use just a DataTable if you don't want to use a DataSet.

Code:
Dim dt as New DataTable
DataAdapter1.Fill(DataTable)
 
See, that's what I mean by me being confused. My table name is ztblAdmin, the field I want the value from is PhoneUserValue
 
Code:
DA_Admin1.Fill(DS_Admin1, "ztblAdmin")

Fill it like that. Or you can even leave off the table name, and just use the Zero index.

You will get whatever columns your query contained.

Code:
strAccess = DS_Admin1.Tables("ztblAdmin").Rows(0).Item(Index of PhoneUserValue column)
[code]
 
RiverGuy,

I can't thank you enough for sticking by me with all of my niave questions. I've figured out what I was doing wrong and it works fine now. Again, thank you for your patience with this question.

Corinne
 
Hope this helps, Corianne, this is using a DataView and and a DataGrid

Dim strConnection as String
'Set your cconnection string to whatever it is
Dim objConnection as SQLConnection
Set objConnection = new SQLConnection(strConnection)
Dim strSQL as String
'Set your SQL Statement
'strSQL = "SELECT LANID, PhoneUser, PhoneUserValue FROM ztblAdmin WHERE LANID = '" & strEmployeeID & "'"
Dim objAdapter as SQLDataAdapter
Set objAdatper = New SQLDataAdapter(strSQL, strConnection)
Dim objDataSet as DataSet
Set objDataSet = New DataSet()
objAdapter.fill(objDataSet, "dtPhoneUsers")
Dim dvLanID as DataView
Set dvLanId = new DataView(objDataSet.Tables("dtPhoneUsers"))

'I will assume you have a datagrid on your form called dgUsers
dgUsers.DataSource = dvLanId
dgUSers.DataBind()

I didn't test this, but that is the logic behind that ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top