-
Jul 28th, 2021, 05:55 PM
#1
Thread Starter
Junior Member
[RESOLVED] VB - Connection string to oracle and feed DataGridview1 based on SQL
Hi all
I'm new to Visual Studio but i have a few little knowledge on VB6.
Here is the problem i'm facing and i don't know what to do after soo many different attempt.
In Server explorer, i'm able to connect to an Oracle database different ways, including like below:
So far, so good.
But my goal is to create a VB form with a button and a DataGridView1. Then, i need to connect to the Oracle database, and run an SQL query where the result will show in the DataGridView1.
The last attempt among many others since few weeks now, i tried:
Code:
Imports System.Data
Imports Oracle.ManagedDataAccess.Client
Imports Oracle.ManagedDataAccess.Types
Then below code:
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim oradb As String = "Data Source=laheotv39:1521/PT334;User Id=xxxxxxxxxxx;Password=xxxxxxxxxxxxxx;"
Dim conn As New OracleConnection(oradb)
conn.Open()
Dim cmd As New OracleCommand With {
.Connection = conn,
.CommandText = "select NAME from carrier_t where carr_cd = 'Dycom'",
.CommandType = CommandType.Text
}
Dim dr As OracleDataReader = cmd.ExecuteReader()
If dr.Read() Then
Label1.Text = dr.Item("NAME")
Else
Label1.Text = "NAME not found"
End If
conn.Dispose()
End Sub
But with this code, i have error:
System.TypeLoadException: 'Could not load type 'System.Security.Principal.WindowsImpersonationContext' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.'
So i'm assuming that i'm not doing it correctly for my connection.
Why would i be able to connect to Oracle on server explorer but not able to connect and pull data to feed a DataGridView?
Do i need extra app or drivers to be downloaded so that it works?
How can i make it work? I really need to have some guidance cause after all the test i tried, i'm more lost then ever right now.
Thanks for your help.
-
Jul 28th, 2021, 06:34 PM
#2
Re: VB - Connection string to oracle and feed DataGridview1 based on SQL
Try using this NuGet package for .NET Framework, for .NET Core this package.
Also get Oracle SQL-Developer, try your connection there.
-
Jul 29th, 2021, 04:43 AM
#3
Thread Starter
Junior Member
Re: VB - Connection string to oracle and feed DataGridview1 based on SQL
This is very good. I'm now able to feed my lable1 using below code.
Code:
Dim oradb As String = "Data Source=laheotv39:1521/PT334;User Id=xxxxxxxxxxx;Password=xxxxxxxxxxxxxx;"
Dim conn As New OracleConnection(oradb)
conn.Open()
Dim cmd As New OracleCommand With {
.Connection = conn,
.CommandText = "select NAME from carrier_t where carr_cd = 'Dycom'",
.CommandType = CommandType.Text
}
Dim dr As OracleDataReader = cmd.ExecuteReader()
If dr.Read() Then
Label1.Text = dr.Item("NAME")
Else
Label1.Text = "NAME not found"
End If
conn.Dispose()
Now how can i feed my DataGridView1.
I've tried with below but did not work:
Code:
DataGridView1.DataSource = dr.Item("NAME")
Also if my query looks like:
Code:
.CommandText = "select * from carrier_t",
How would i see the full table result in the DataGridView1?
Last edited by Wilder1234; Jul 29th, 2021 at 04:59 AM.
-
Jul 31st, 2021, 08:48 AM
#4
Re: VB - Connection string to oracle and feed DataGridview1 based on SQL
Take a look at the following C# project.
VB.NET is here
-
Jul 31st, 2021, 03:57 PM
#5
Thread Starter
Junior Member
Re: VB - Connection string to oracle and feed DataGridview1 based on SQL
Thank you so much. I will try it Monday and let you know if i have any questions.
-
Jul 31st, 2021, 06:09 PM
#6
Re: VB - Connection string to oracle and feed DataGridview1 based on SQL
Karen Payne shows you how to get a DataTable. That, you can bind to your DataGridView
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 31st, 2021, 07:55 PM
#7
Thread Starter
Junior Member
Re: VB - Connection string to oracle and feed DataGridview1 based on SQL
Thank's Paul. I look forward to trying it out on Monday. I will use the VB.Net version.
-
Jul 31st, 2021, 09:16 PM
#8
Re: VB - Connection string to oracle and feed DataGridview1 based on SQL
Originally Posted by kareninstructor
Take a look at the following C# project.
VB.NET is here
That could be accomplished more simply without the Async stuff...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 1st, 2021, 06:07 AM
#9
Thread Starter
Junior Member
Re: VB - Connection string to oracle and feed DataGridview1 based on SQL
.Paul.
How do you suggest I do this exactly?
-
Aug 1st, 2021, 06:48 AM
#10
Re: VB - Connection string to oracle and feed DataGridview1 based on SQL
Ok. What do you want in Lable1? Just the 'Name' from the first row, or changing depending on which row is selected?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 1st, 2021, 06:52 AM
#11
Re: VB - Connection string to oracle and feed DataGridview1 based on SQL
Code:
Dim oradb As String = "Data Source=laheotv39:1521/PT334;User Id=xxxxxxxxxxx;Password=xxxxxxxxxxxxxx;"
Dim conn As New OracleConnection(oradb)
conn.Open()
Dim cmd As New OracleCommand With {
.Connection = conn,
.CommandText = "select NAME from carrier_t where carr_cd = 'Dycom'",
.CommandType = CommandType.Text
}
Dim dt As New DataTable
dt.Load(cmd.ExecuteReader())
DataGridView1.DataSource = dt
Label1.DataBindings.Add("Text", dt, "NAME")
conn.Dispose()
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 1st, 2021, 07:18 AM
#12
Thread Starter
Junior Member
Re: VB - Connection string to oracle and feed DataGridview1 based on SQL
I will try this and let you know.
If it's only to use DataGridView1.DataSource = dt, that look pretty straight forward.
Thanks .Paul.
Last edited by Wilder1234; Aug 1st, 2021 at 07:46 AM.
-
Aug 1st, 2021, 07:46 AM
#13
Re: VB - Connection string to oracle and feed DataGridview1 based on SQL
Yeah, load dt as I showed you, then set your dgv datasource.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 1st, 2021, 08:15 AM
#14
Thread Starter
Junior Member
Re: VB - Connection string to oracle and feed DataGridview1 based on SQL
I was too curious and i've tried it a few minutes ago. I confirm that this is working great with:
Code:
DataGridView1.DataSource = dt
Thanks Karen Payne and Paul for your help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|