473,471 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

basic dataadapter/dataset question

Hi,

I have a dataadapter that contains just a single record.

Dim SqlDataAdapterValues As New SqlDataAdapter("select * from
tbl_formatvalue where format_id = " & intFormatInt, dbConnectionIn)
SqlDataAdapterJobs.Fill(dsFormats, "formatvalues")

I want to get the value for column customer_id from this dataset.

usually i would have a dataset with many records, and just do a for each
..... looping through and processing each row.

In this case, as there is only one record in the dataset, is there a better
way to get to the data that using the for each?

Or.. is there a better way of getting my data, is a dataset in this
situation not the best.
Thanks

Nov 10 '08 #1
3 2296

DataSets do not do "singles" very well.

If you create a strong dataset.....you can get at the row like this
(sorry I can only do c# from memory)

Let's say I have a strong dataset called OrganizationDS
and I have "Employee" as a table. EmpID (int) adn SSN (string)

OrganizationDS ds = new OrganizationDS();
ds.Employee.AddNewEmployeeRow ( 101 , "222334444" );

if (null!=ds)
{
if(null!= ds.Employee)
{
if(ds.Employee.Count>0) //<<this might be the Length property
OrganizationDS.EmployeeRow row = ds.Employee[0];
if(null!= row)
{
Console.WriteLine ( row.EmpID ) ;
Console.WriteLine ( row.SSN ) ;

}

//also use the Select
DataRow[] rows = ds.Employee.Select("EmpID=101");
if (null!=rows)
{
if(rows.Length>0)
{
OrganizationDS.EmployeeRow anotherRow = rows[0] as
OrganizationDS.EmployeeRow;
if(null!= anotherRow )
{
Console.WriteLine ( anotherRow .EmpID ) ;
Console.WriteLine ( anotherRow .SSN ) ;

}

}
}

}
}


Here is a GREAT article on different approaches:
http://msdn.microsoft.com/en-us/library/ms978496.aspx
Bookmark it, read it every 3 months (for 2 years) since you are new.
It wont' "take" the first time, but will make more and more sense as you
develop.

Which is where you will read this:
Passing DataSets As Inputs and Outputs
The disadvantages of this option are as follows:
* Representation of a single business entity
"Newbie" <ne****@nospam.comwrote in message
news:Ou**************@TK2MSFTNGP02.phx.gbl...
Hi,

I have a dataadapter that contains just a single record.

Dim SqlDataAdapterValues As New SqlDataAdapter("select * from
tbl_formatvalue where format_id = " & intFormatInt, dbConnectionIn)
SqlDataAdapterJobs.Fill(dsFormats, "formatvalues")

I want to get the value for column customer_id from this dataset.

usually i would have a dataset with many records, and just do a for each
.... looping through and processing each row.

In this case, as there is only one record in the dataset, is there a
better way to get to the data that using the for each?

Or.. is there a better way of getting my data, is a dataset in this
situation not the best.
Thanks

Nov 10 '08 #2
Here is how you perform a search on a table in a dataset

Dim drF() As DataRow
drF = dsMain.Tables("MyTable").Select("ID = " & strID)

For Each row As Datarow in drF
console.writeline(row("ID").toString & " " & row("Name"))
Next

you can replace the .Select("ID = ...
with .Select("FirstDate = '" & someStringDate & "'")

The .Select property takes a regular string for the Where part of a sql
statement.

.Select("ID = 1")

.Select("ID = " & someIntVar.ToString)
Rich

*** Sent via Developersdex http://www.developersdex.com ***
Nov 10 '08 #3
Newbie wrote:
<snip>
I have a dataadapter that contains just a single record.

Dim SqlDataAdapterValues As New SqlDataAdapter("select * from
tbl_formatvalue where format_id = " & intFormatInt, dbConnectionIn)
SqlDataAdapterJobs.Fill(dsFormats, "formatvalues")

I want to get the value for column customer_id from this dataset.
<snip>

If you only want one customer_id from the query, you could use the
ExecuteScalar method of the SqlCommand:

<example>
'Prepare the command
Dim Cmd As New SqlCommand( _
"select top 1 customer_id " _
& "from tbl_formatvalue " _
& "where format_id = @format_id")

Cmd.Parameters.Add("@format_id")
Cmd.Connection = dbConnectionIn

'... elsewhere in your code

'specify the parameter value
Cmd.Parameters("@format_id").Value = intFormatInt

'fetch the data ad convert the result to integer
Dim CustID As Integer = Ctype(Cmd.ExecuteScalar, Integer)
</example>

Of course, you could build a class to encapsulate all that and have a
method to return the ID already converted to integer and where access
errors etc, would be dealt with...

Dim CustID As Integer = SomeClass.GetCustomerID(intFormatInt)
Hope this helps.

Regards,

Branco.
Nov 11 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Stephen Noronha | last post by:
I have a question, please correct me if I am wrong. I am assuming that a dataadapter establishes a connection and after filling the dataset or datatable or whatever, will close the connection to...
13
by: Doug Bell | last post by:
Hi, I thought I had this sorted this morning but it is still a problem. My application has a DataAccess Class. When it starts, it: Connects to a DB (OLE DB) If it connects it uses an...
8
by: Zorpiedoman | last post by:
I keep getting a concurrency exception the second time I make a change and attempt to update a dataadapter. It appears this is by design, so there must be something I can do to avoid it. ...
0
by: davefromalbury | last post by:
I have a gridview connected to a objectdatasource. Can't for the life of me get it to display anything though, always appears empty. The dataset always seems to be filled with records, as the...
2
by: susan.f.barrett | last post by:
Hi, Despite me being able to type the following in to SQL Server and it updating 1 row: > updatestockcategory 1093, 839 In my code, it is not updating any rows. dataSet = new DataSet();
7
by: Max | last post by:
I've included the needed tables in the DataSource. Those tables that are bound to controls I can workwith. But how do you get access to the DataAdaptors that are not bound? me.Dataset1.table...
3
by: Fred Chateau | last post by:
Any obvious reason here why data is not being loaded into the database? SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlCommand); SqlCommandBuilder commandBuilder = new...
1
by: pate20 | last post by:
I'm modifing a unfinished form which created by someone else, and tying it to a database which dataadapter, datasource and a Dataset. I have been goofing around with this forms and code. So far...
2
by: BobLewiston | last post by:
Some of you may have seen my earlier thread “PasswordHash NULL problem”. I’ve started a new thread because investigation has shown that the problem is actually quite different than I previously...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.