Fetch output of DBMS_OUTPUT.PUT_LINE in a VB .net application - vb.net

I am trying to get a simple hello world output in vb from a pl/sql anonymous block
As far as I tried
Imports System.Data
Imports Oracle.DataAccess.Client
Imports Oracle.DataAccess.Types
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oradb As String = "Data Source=(DESCRIPTION=" _
+ "(ADDRESS=(PROTOCOL=TCP)(HOST=vmDA2D319)(PORT=1521))" _
+ "(CONNECT_DATA=(SERVICE_NAME=XE)));" _
+ "User Id=hr;Password=hr;"
Dim conn As New OracleConnection(oradb)
conn.Open()
Dim cmd As New OracleCommand
cmd.Connection = conn
cmd.CommandText = "set server output on;" & vbNewLine & "DECLARE" & vbNewLine & "message varchar2(20):= 'Hello, World!'; " & vbNewLine & "BEGIN()" & vbNewLine & "dbms_output.put_line(message);" & vbNewLine & "END;" & vbNewLine & "/"
cmd.CommandType = CommandType.Text
Dim dr As OracleDataReader = cmd.ExecuteReader()
dr.Read()
Label1.Text = dr.ExecuteReader
conn.Dispose()
End Sub
End Class
Now this is the Part where I am stuck now. I just want fetch the output in a textbox actually.
But I am just trying to get the output in a label or maybe in a messagebox first.
UPDATE
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oradb As String = "Data Source=(DESCRIPTION=" _
+ "(ADDRESS=(PROTOCOL=TCP)(HOST=vmDA2D319)(PORT=1521))" _
+ "(CONNECT_DATA=(SERVICE_NAME=XE)));" _
+ "User Id=hr;Password=hr;"
Dim conn As New OracleConnection(oradb)
conn.Open()
Dim cmd As New OracleCommand
cmd.CommandText = "set server output on;" & vbNewLine & "DECLARE" & vbNewLine & "return_value VARCHAR2(100) := 'Hello World!';" & vbNewLine & "get_status INTEGER;" & vbNewLine & "BEGIN" & vbNewLine & "DBMS_OUTPUT.GET_LINE (return_value, get_status);" & vbNewLine & "IF get_status = 0" & vbNewLine & "THEN" & vbNewLine & "RETURN return_value;" & vbNewLine & "ELSE" & vbNewLine & "RETURN NULL;" & vbNewLine & "END IF;" & vbNewLine & "END;" & vbNewLine & "/"
cmd.CommandType = CommandType.Text
cmd.Parameters.Add("return_value", OracleDbType.Varchar2, 100)
cmd.Parameters("return_value").Direction = ParameterDirection.Output
cmd.Connection = conn
cmd.ExecuteScalar()
Label1.Text = cmd.Parameters("return_value").Value
conn.Dispose()
End Sub
I am getting error at cmd.ExecuteScalar()
with ORA-00922: missing or invalid option

Related

How to using Linq with Dataview?

I am learning linq to dataset from this post: https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/creating-a-dataview-object-linq-to-dataset
and I am having trouble when running this code, I do not understand why it doesn't work, however
The code works fine if I delete this line:
Where order1.Field(Of Integer)("Column2") = 1
my table have 4 columns corresponds to the name: Column1,2,3,4
Here is vbcode:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim DtSet As System.Data.DataSet = New System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source='C:\Users\Username\Downloads\Danh STT.xlsx';Extended Properties='Excel 12.0 Xml;HDR=YES;'")
MyCommand = New System.Data.OleDb.OleDbDataAdapter _
("select * from [Sheet1$]", MyConnection)
MyCommand.TableMappings.Add("Author", "TestTable")
MyCommand.Fill(DtSet)
Dim orders As DataTable = DtSet.Tables(0)
Dim query = From order1 In orders.AsEnumerable
Order By order1.Field(Of String)("Column1")
Where order1.Field(Of Integer)("Column2") = 1
Select order1
Dim view As System.Data.DataView = New Data.DataView
view = query.AsDataView()
DataGridView1.DataSource = view
MyConnection.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Can you explain for me please!
Thanks a lot.
Edit: [The error I received]
{System.InvalidCastException: Specified cast is not valid." & vbCrLf & " at System.Data.DataRowExtensions.UnboxT1.ValueField(Object value)" & vbCrLf & " at System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)" & vbCrLf & " at Dataview.Form1._Closure$__._Lambda$__1-0(DataRow order1) in D:\2.0_VSTO_PROJECT\D_1_MY_PROJECT\1 EXCEL\.NET\1 Class .Net\LinqExample\Dataview\Dataview\Form1.vb:line 31" & vbCrLf & " at System.Data.EnumerableRowCollection1.b__16_0(DataRow row)" & vbCrLf & " at System.Data.EnumerableRowCollection1.<>c__DisplayClass16_0.<GetLinqDataView>b__3(DataRow row)" & vbCrLf & " at System.Data.DataView.RowPredicateFilter.System.Data.IFilter.Invoke(DataRow row, DataRowVersion version)" & vbCrLf & " at System.Data.Index.AcceptRecord(Int32 record, IFilter filter)" & vbCrLf & " at System.Data.Index.InitRecords(IFilter filter)" & vbCrLf & " at System.Data.Index..ctor(DataTable table, IndexField[] indexFields, Comparison1 comparison, DataViewRowState recordStates, IFilter rowFilter)" & vbCrLf & " at System.Data.DataView.UpdateIndex(Boolean force, Boolean fireEvent)" & vbCrLf & " at System.Data.DataView.UpdateIndex(Boolean force)" & vbCrLf & " at System.Data.DataView.SetIndex2(String newSort, DataViewRowState newRowStates, IFilter newRowFilter, Boolean fireEvent)" & vbCrLf & " at System.Data.DataView..ctor(DataTable table, Predicate1 predicate, Comparison1 comparison, DataViewRowState RowState)" & vbCrLf & " at System.Data.EnumerableRowCollection1.GetLinqDataView()" & vbCrLf & " at System.Data.DataTableExtensions.AsDataView[T](EnumerableRowCollection1 source)" & vbCrLf & " at Dataview.Form1.Button1_Click(Object sender, EventArgs e) in D:\2.0_VSTO_PROJECT\D_1_MY_PROJECT\1 EXCEL.NET\1 Class .Net\LinqExample\Dataview\Dataview\Form1.vb:line 36}

syntax error insert into statement vb.net

pls help solve me this question.. im very new to this
i can't add new employee to the table employee.. whenever i try to add it shows syntax error insert into statement
Public Class AddNewEmployee
Dim dr As OleDbDataReader
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim conn As New OleDbConnection(My.Settings.rayshadatabaseConnectionString)
Dim cmd As OleDbCommand
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
conn.Open()
Try
Dim str As String = "INSERT INTO employee" _
& "(Employee Name, IC Number, HP Number, Address)" _
& " Values (" _
& "'" & txtEmployeeName.Text & "', " _
& "'" & txtIC_Number.Text & "'," _
& "'" & txtHP_Number.Text & "'," _
& "'" & txtAddress.Text & "')"
cmd = New OleDbCommand(str, conn)
Dim i As Integer = cmd.ExecuteNonQuery()
If i > 0 Then
MessageBox.Show("Record Succesfully added.", "Process Completed", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Adding failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
conn.Close()
cmd.Dispose()
End Try
frmEmployee.loadR()
Me.Close()
End Sub
End Class
Replace this,
Dim str As String = "INSERT INTO employee" _
& "(Employee Name, IC Number, HP Number, Address)" _
& " Values (" _
& "'" & txtEmployeeName.Text & "', " _
& "'" & txtIC_Number.Text & "'," _
& "'" & txtHP_Number.Text & "'," _
& "'" & txtAddress.Text & "')"
with this,
Dim str As String = "INSERT INTO employee" _
& "([Employee Name], [IC Number], [HP Number], [Address])" _
& " Values (" _
& "'" & txtEmployeeName.Text & "', " _
& "'" & txtIC_Number.Text & "'," _
& "'" & txtHP_Number.Text & "'," _
& "'" & txtAddress.Text & "')"
Thanks
Manoj

creating tables in Access using VB.NET

I'm having trouble creating Access tables from VB.NET.
This is the code I have come up with but I keep getting errors:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
'connection string
Dim dbpath As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
dbpath = New Uri(dbpath).LocalPath
Dim my_connection As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\GhostDrive\Desktop\database.mdb"
Dim userTables As DataTable = Nothing
Dim connection As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection()
Dim source As String
'query string
Dim my_query As String = "CREATE TABLE " & TextBox2.Text & " ( " & _
"ID Counter, " & _
"Year datetime," & _
"Title varchar(40)," & _
"image1 Long," & _
"Image2 Long," & _
"Image3 Long," & _
"Image4 Long," & _
"Serial varchar(20)," & _
"Purchaseprice Currency," & _
"Evalprice Currency, " & _
"Datepurchase DateTime, " & _
"Dateeval DateTime, " & _
"Sign varchar(40), " & _
"Grading varchar(20)," & _
"Eval YesNo, " & _
"Star YesNo, " & _
"Folder YesNo, " & _
"Forsale YesNo, " & _
"Error YesNo, " & _
"Barcode(varchar(20)," & _
"Comm YesNo )"
'create a connection
Dim my_dbConnection As New OleDbConnection(my_connection)
'create a command
Dim my_Command As New OleDbCommand(my_query, my_dbConnection)
'connection open
my_dbConnection.Open()
'command execute
my_Command.ExecuteNonQuery()
'close connection
my_dbConnection.Close()
ListBox1.Items.Clear()
source = TextBox1.Text
connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + source
Dim restrictions() As String = New String(3) {}
restrictions(3) = "Table"
connection.Open()
' Get list of user tables
userTables = connection.GetSchema("Tables", restrictions)
connection.Close()
' Add list of table names to listBox
Dim i As Integer
For i = 0 To userTables.Rows.Count - 1 Step i + 1
ListBox1.Items.Add(userTables.Rows(i)(2).ToString())
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
if i take all them out except ID it works and then i add them on by on back it stops "Year datetime," & _
YEAR is a reserved word in Access SQL. If I try to run the following code ...
Dim connectionString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Users\Public\mdbTest.mdb;"
Using con As New OleDbConnection(connectionString)
con.Open()
Using cmd As New OleDbCommand()
cmd.Connection = con
cmd.CommandText = "CREATE TABLE zzzTest (ID COUNTER, Year INTEGER)"
Try
cmd.ExecuteNonQuery()
Console.WriteLine("Table created.")
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Using
con.Close()
End Using
... I get
Syntax error in field definition.
However, if I enclose the field name in square brackets ...
cmd.CommandText = "CREATE TABLE zzzTest (ID COUNTER, [Year] INTEGER)"
... then I get
Table created.

The CommandText property has not been properly initialized

i am using vb.net and mysqladmin as my database.
i have a problem in my codes, and i don't know how to debug it.
please help me..
my problem is when i click the button update the error shows
"The CommandText property has not been properly initialized."
this is the codes:
Dim intDB_ID_Selected As Integer
'Private Sub cmdupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdupdate.Click
If MessageBox.Show("Do you want to update this record?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = vbYes Then
Dim sqlcommand As New MySqlCommand("UPDATE user_info " & _
" SET name = '" & txtname.Text.Trim & "'," & _
" address = '" & txtaddress.Text.Trim & "', " & _
" age = '" & txtage.Text.Trim & "', " & _
" WHERE id= '" & intDB_ID_Selected & "'", sConnection)
Call execCmd(SQL)
load1()
MsgBox("Record updated successfully.", MsgBoxStyle.Information)
End If
End Sub `
Public Sub execCmd(ByVal PstrSQL As String)
With cmd
.CommandText = PstrSQL
.ExecuteNonQuery()
End With
End Sub
the error line is in
.ExecuteNonQuery()
i am a beginner in this language, so please help me. im begging you guys!!
what is cmd and has it been initialized?
Oh, and try to used parameterized query. The earlier you get into the habit, the better.
Try doing this instead after the messagebox selection. cmd is not properly initialized in execCmd.
Dim sqlStr as String = "UPDATE user_info " & _
" SET name = '" & txtname.Text.Trim & "'," & _
" address = '" & txtaddress.Text.Trim & "', " & _
" age = '" & txtage.Text.Trim & "', " & _
" WHERE id= '" & intDB_ID_Selected & "'", sConnection)
Dim sqlcommand As New MySqlCommand(sqlStr)
sqlcommand.ExecuteNonQuery()
load1()
MsgBox("Record updated successfully.", MsgBoxStyle.Information)

Visual Studio 2010 insert syntax error again and again while inserting value to .mdb file

what is missing in insert statement ? the full code is now here. i cant add new fields values to .mdb file. it took 3 days to develop this app and now its not running.
what is missing in insert statement ? the full code is now here. i cant add new fields values to .mdb file. it took 3 days to develop this app and now its not running.
Public Class client
Dim cnn As New OleDb.OleDbConnection
Private Sub reloaddata()
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
Dim da As New OleDb.OleDbDataAdapter("select * from clientsData", cnn)
Dim dt As New DataTable
da.Fill(dt)
Me.cview.DataSource = dt
cnn.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
display.Hide()
cnn = New OleDb.OleDbConnection
cnn.ConnectionString = "provider= microsoft.jet.oledb.4.0; data source=" & Application.StartupPath & "\clients.mdb"
Me.reloaddata()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
display.Show()
End Sub
Private Sub cview_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles cview.CellClick
If cnn.State = ConnectionState.Closed Then
cnn.Open()
End If
Dim i As Integer
i = cview.CurrentRow.Index
If cview.CurrentCell.Value Is Nothing Then
MsgBox("Empty Field")
Else
view.lbl1.Text = cview.Item(0, i).Value.ToString
view.lbl2.Text = cview.Item(1, i).Value.ToString
view.lbl3.Text = cview.Item(2, i).Value.ToString
view.lbl4.Text = cview.Item(3, i).Value.ToString
view.lbl5.Text = cview.Item(4, i).Value.ToString
view.lbl6.Text = cview.Item(5, i).Value.ToString
view.lbl7.Text = cview.Item(6, i).Value.ToString
view.lbl8.Text = cview.Item(11, i).Value.ToString
view.lbl9.Text = cview.Item(12, i).Value.ToString
view.lbl10.Text = cview.Item(7, i).Value.ToString
view.lbl11.Text = cview.Item(8, i).Value.ToString
view.lbl12.Text = cview.Item(9, i).Value.ToString
view.lbl13.Text = cview.Item(10, i).Value.ToString
view.lbl14.Text = cview.Item(13, i).Value.ToString
view.Show()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim cmd As New OleDb.OleDbCommand
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
cmd.Connection = cnn
cmd.CommandText = "insert into clientsdata(ID,Client,Project,Domain,Hosting,bulk sms,maintenance,Order date,amount,last billing,next billing,username,password,due amount) VALUES ('" & Me.cid.Text & "','" & Me.cname.Text & "','" & Me.cproj.Text & "','" & Me.cdmn.Text & "','" & Me.chost.Text & "','" & Me.csms.Text & "','" & Me.cmain.Text & "','" & Me.codt.Text & "','" & Me.camnt.Text & "','" & Me.cldt.Text & "','" & Me.cndt.Text & "','" & Me.cuid.Text & "','" & Me.cpass.Text & "','" & Me.cdue.Text & "' )"
cmd.ExecuteNonQuery()
Me.reloaddata()
cnn.Close()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
cid.Text = "cid"
cname.Text = "cname"
cproj.Text = "cpro"
cdmn.Text = "domain"
chost.Text = "chost"
csms.Text = "sms"
cmain.Text = "main"
codt.Text = "codt"
camnt.Text = "mount"
cldt.Text = "last"
cndt.Text = "next"
cdue.Text = "due"
cuid.Text = "uid"
cpass.Text = "pass"
End Sub
End Class
ok problem solved !! I just added [ ] in the fields.
source: (VB.NET)Syntax Error in INSERT INTO statement - MICROSOFT JET DATABASE ENGINE
cmd.CommandText = "insert into clientsdata([ID],[Client],[Project],[Domain],[Hosting],[bulk sms],[maintenance],[Order date],[amount],[last billing],[next billing],[username],[password],[due amount]) VALUES (" & Me.cid.Text & ",'" & Me.cname.Text & "','" & Me.cproj.Text & "','" & Me.cdmn.Text & "','" & Me.chost.Text & "','" & Me.csms.Text & "','" & Me.cmain.Text & "','" & Me.codt.Text & "','" & Me.camnt.Text & "','" & Me.cldt.Text & "','" & Me.cndt.Text & "','" & Me.cuid.Text & "','" & Me.cpass.Text & "','" & Me.cdue.Text & "' )"
Your date field is a keyword. You have to place it with brackets: [date]. Also, any field names that have spaces require brackets, too: [Order date].
You really should use parameters to avoid SQL injection and to solve a host of other issues with updating databases.
I would also avoid trying to manage the connection state of the database. Just use the Using syntax so that the connection always closes.
If Me.cid.Text = "" Then
MessageBox.Show("Please input values")
Else
Using con As New OleDb.OleDbConnection("...")
con.Open()
Using cmd As New OleDb.OleDbCommand()
cmd.Connection = con
cmd.CommandText = "..."
cmd.Parameters.AddWithValue("#ID", Me.cid.Text)
cmd.Parameters.AddWithValue(...more)
cmd.ExecuteNonQuery()
End Using
End Using
reloaddata()
End If
It's hard to say what the problem is without knowing what is contained in each of the .Text values, and without seeing the definition of the table you are filling in. But I could hazard a guess that the Me.camnt.Text and Me.cdue.Text probably don't want to be surrounded by single quotes; that is to say, you may need something like this...
Me.codt.Text & "'," & Me.camnt.Text & ",'" & Me.cldt.Text
and
Me.cndt.Text & "'," & Me.cdue.Text & ",'" & Me.cuid.Text