Question Setting up dataset in vb code and querying dataset

jsmithfw

Member
Joined
May 24, 2009
Messages
5
Programming Experience
Beginner
I have a table (tblSpanishVerbs) of spanish verbs and need a function to return the corresponding English verb after passing the Spanish verb. For example, from below i want to pass the verb "comer" and return the verb "eat".

I am using Visual Studio Express 2008 and have imported a SQL Server database in to the project.

ID_verb SpanishInfinitive EnglishInfinitive
1 hablar speak
2 comer eat
3 partir leave
4 dar give
5 aprender learn

I am attempting to create a dataset and query the dataset with SQL with "SELECT * FROM tblSpanishVerbs WHERE SpanishInfinitive = 'comer'".

Here's my attempt with VBglish.

Public Function GetEnglishInfinitive(ByVal mySpanishInfinitive As String)
Dim myEnglishInfinitive As String = ""
Dim ds As New DataSet

'???How do i set up the dataset DS

For Each r As DataRow In ds.Tables(0).Rows
If r.Item("SpanishInfinitive").ToString = mySpanishInfinitive Then
TextBox1.Text = r.Item("EnglishInfinitive").ToString
Exit For
End If
Next

Return myEnglishInfinitive
End Function

Struggling with this basic concept so hope you can help.

Thanks!
John
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,370
Location
Sydney, Australia
Programming Experience
10+
VB.NET:
Dim spanish As String = "comer"
Dim rows As DataRow() = ds.Tables(0).Select(String.Format("SpanishInfinitive = '{0}'", spanish)

If rows.Length > 0 Then
    Dim english As String = CStr(row("EnglishInfinitive"))

    'Use english here.
End If
 

jsmithfw

Member
Joined
May 24, 2009
Messages
5
Programming Experience
Beginner
And how do I set up the dataset ds? So far I have been using the controls/dataset builder in visual studio on forms. I want this code to run in a functions module so can't use these tools there.

Thanks for you help.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,370
Location
Sydney, Australia
Programming Experience
10+
Just as you can drag a Button onto a form in the designer or create one in code, so to you can drag a DataSet onto a form in the designer or create one in code. You create an instance of your DataSet in code the very same way you create an instance of any class in code.

That said, if you've only got one table of data then just use a DataTable on its own. No need for a DataSet to hold one DataTable.
 

jsmithfw

Member
Joined
May 24, 2009
Messages
5
Programming Experience
Beginner
I can work with a datasets on forms ok.

I am trying to load a dataset using code alone as the function needs to be run from a couple of forms.

I am only running the function on 1 table so I could use a datatable rather than dataset.

So I guess I need

Dim dt as new datatable

But how do load the table I want in to dt?

Also when I type Cstr(Row("..... - it gives the error "Row is not declared".
 

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,052
Programming Experience
10+
I have a table (tblSpanishVerbs) of spanish verbs and need a function to return the corresponding English verb after passing the Spanish verb. For example, from below i want to pass the verb "comer" and return the verb "eat".

I am using Visual Studio Express 2008 and have imported a SQL Server database in to the project.

ID_verb SpanishInfinitive EnglishInfinitive
1 hablar speak
2 comer eat
3 partir leave
4 dar give
5 aprender learn

I am attempting to create a dataset and query the dataset with SQL with "SELECT * FROM tblSpanishVerbs WHERE SpanishInfinitive = 'comer'".

Here's my attempt with VBglish.

Public Function GetEnglishInfinitive(ByVal mySpanishInfinitive As String)
Dim myEnglishInfinitive As String = ""
Dim ds As New DataSet

'???How do i set up the dataset DS

For Each r As DataRow In ds.Tables(0).Rows
If r.Item("SpanishInfinitive").ToString = mySpanishInfinitive Then
TextBox1.Text = r.Item("EnglishInfinitive").ToString
Exit For
End If
Next

Return myEnglishInfinitive
End Function

Struggling with this basic concept so hope you can help.

Thanks!
John


Take a read of the DW2 link in my signature, titled "Creating a Form to Search Data"

At the moment it looks like you're downloading the entire database into your client app and then using a slow search routine locally; databases were designed to hold and search data.. so use the best tool for the job
 
Top Bottom