how to fill system.data.dataset from oracle.dataacess.client.oracledataadapter? - vb.net

I have installed Oracle 10g Express Edition database on a server and install the client on my PC.
Now, I`m developing a vb.net application using visual studio 2005 and I need to use the oracle 10g express edition database. So I initialize the connection using the following connection string:
_connectionString = "User Id=Graphya;Password=Graphya;Data Source=gis64:1522/XE;"
Then I define new OracleDataAdapter, and I use the following code to fill a dataset:
Dim insertCommand As OracleCommand = New OracleCommand()
Dim commandTextTemplate As String = "INSERT INTO {0}(" & g_pfldUsername & ", " & g_pfldSubject & ") VALUES (?, ?)"
insertCommand.CommandText = String.Format(commandTextTemplate,TABLE_NAME)
insertCommand.Connection = Me.Connection
insertCommand.Parameters.Add(New Oracle.DataAccess.Client.OracleParameter(g_pfldUsername, Oracle.DataAccess.Client.OracleDbType.Varchar2, 50, g_pfldUsername))
insertCommand.Parameters.Add(New Oracle.DataAccess.Client.OracleParameter(g_pfldSubject, Oracle.DataAccess.Client.OracleDbType.Varchar2, 50, g_pfldSubject))
_OracleDataAdapter.InsertCommand = insertCommand
_OracleDataAdapter.Fill(_dataSet, TABLE_NAME)
So after debugging this code I got the following error:
Unable to cast object of type 'Oracle.DataAccess.Client.OracleCommand' to type 'System.Data.Common.DbCommand'.

#Davideg: my code is c# to fill data set
OleDbConnection cnOra = new OleDbConnection("Provider=MSDAORA;Data Source=myOracleServer;"
+ "user id=myUID;password=myPWD;"
+ "persist security info=false;");
OleDbCommand cmdPerson = new OleDbCommand
+ ("{call PackPerson.allPerson({resultset 3, ssn, fname, lname})}", cnOra);
OleDbDataAdapter daPerson = new OleDbDataAdapter(cmdPerson);
cnOra.Open();
DataSet ds = new DataSet();
daPerson.Fill(ds,"Person");
this.dataGrid1.DataSource = ds.Tables["Person"];
cnOra.Close();

Function GetEmailsByPageName(ByVal pageName As String) As DataSet
Dim cn As New OracleConnection
Dim cmd As New OracleCommand
cn = New OracleConnection
cn.ConnectionString = (ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
cmd = New OracleCommand("ORACLEDBA.PACKAGE_EMAIL.SP_EMAIL_LISTING_BY_NAME")
cmd.CommandType = Data.CommandType.StoredProcedure
cmd.Connection = cn
cmd.BindByName = True
Dim paramCursor As OracleParameter = New OracleParameter("email_list_cursor", OracleDbType.RefCursor)
With cmd.Parameters
.Add(New OracleParameter("a_page_name", OracleDbType.Varchar2)).Value = pageName
.Add("a_err_code", OracleDbType.Int32, Data.ParameterDirection.Output)
.Add("a_err_msg", OracleDbType.Varchar2, 300).Direction = Data.ParameterDirection.Output
.Add(paramCursor).Direction = Data.ParameterDirection.Output
End With
Dim da As New OracleDataAdapter(cmd)
Dim dsEmail As DataSet = New DataSet
Try
da.SelectCommand = cmd
da.Fill(dsEmail)
Return dsEmail
Catch ex As Exception
Throw
Finally
da.Dispose()
cmd.Dispose()
cn.Dispose()
If cn.State = ConnectionState.Open Then
cn.Close()
End If
End Try
End Function

Related

sqlite index name not working

i am working on windows mobile application.i was using sqlserverce database in my mobile application.below mentioned my working code
Dim conn1 As New SqlServerCe.SqlCeConnection("Data Source = " & hht_Storage & "\database\master.sdf")
Dim cmd1 As SqlServerCe.SqlCeCommand = conn1.CreateCommand()
Dim rdr1 As SqlServerCe.SqlCeDataReader
conn.Open()
cmd.CommandType = CommandType.TableDirect
cmd1.IndexName = "barcodeidx"
cmd.CommandText = "barcode"
rdr = cmd.ExecuteReader()
MsgBox(Trim(Txt_barcode.Text))
rslt = rdr1.Seek(SqlServerCe.DbSeekOptions.FirstEqual, New Object() {Trim(Txt_barcode.Text)})
now i changed my database to sqlite and started coding..so i given code like this.
Dim conn As New SQLite.SQLiteConnection("Data Source = " & hht_Storage & "\database\master.s3db")
Dim cmd As SQLite.SQLiteCommand = conn.CreateCommand()
Dim rdr As SQLite.SQLiteDataReader
cmd.CommandType = CommandType.TableDirect
cmd.IndexName = "barcodeidx" **'Error' Indexname is not member of System.data.sqlite.sqlitecommand**
cmd.CommandText = "barcode"
rdr = cmd.ExecuteReader()
MsgBox(Trim(Txt_barcode.Text))
rslt = rdr.Seek(SqlServerCe.DbSeekOptions.FirstEqual, New Object() {Trim(Txt_barcode.Text)})
'Error' seek is not member of System.data.sqlite.sqlitecommand
how i can rectify this issue? what should i give instead of this?

Trying to use grid view with my data, but it outputs an error

Following is the code am using to bind the grid, can anyone please suggest me what am doing wrong in this snippet
Dim con As New SqlClient.SqlConnection
Dim ds As New DataSet
Dim adapter As SqlDataAdapter
Dim sql As String
con.ConnectionString = "Data Source=SHAHRUKH-PC\SQLEXPRESS;Initial Catalog=vb;User ID=sa;Password=sa#1234"
con.Open()
sql = "Data Source=SHAHRUKH-PC\SQLEXPRESS;Initial Catalog=vb;User ID=****;Password=*****"
cmd.CommandText = "select * From demo_vb Where ID = '" & txtbox4.Text & "'"
adapter = New SqlClient.SqlDataAdapter
adapter.Fill(ds)
GridView1.ItemsSource = New DataView()
GridView1.DisplayMemberPath = "ID"
con.Close()
I get this error:
The SelectCommand property has not been initialized before calling 'Fill'
try following code:
SqlCommand cmd;
SqlConnection con = new SqlConnection("Data Source=SUNTECH-PC\\SQLEXPRESS;Initial Catalog=iSense;Integrated Security=True;");
con.Open();
cmd = new SqlCommand("Select Picture from Images",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
I think this would help
Dim con As New SqlClient.SqlConnection("Data Source=SHAHRUKH-PC\SQLEXPRESS;Initial Catalog=vb;User ID=sa;Password=sa#1234")
Dim ds As New DataSet
Dim sql As New SqlCommand("select * From demo_vb Where ID = '" & txtbox4.Text & "'",con)
Dim adapter As New SqlDataAdapter(sql)
con.Open()
adapter.Fill(ds)
GridView1.datasource = ds.Tables(0)
GridView1.databind()
con.Close()

between two date to show report crystal vb

Dim rpt As New CheckoutReportpreview
Dim cn As SqlConnection
Dim cmd As New SqlCommand
Dim myDA As New SqlDataAdapter
Dim ds As New CheckoutSet
Dim crConnectionInfo As New ConnectionInfo
Try
With crConnectionInfo
.ServerName = "."
.DatabaseName = "Hotel Management System"
.UserID = "sa"
.Password = "s123"
End With
cmd.Connection = cn
cn.Open()
Dim da As New SqlDataAdapter("select * from tblCheckout where CheckoutDate>='" & CDate(Date_from.Value) & "' and CheckoutDate<='" & CDate(Date_to.Value) & "' ", cn)
cmd.Prepare()
cmd.CommandType = CommandType.Text
myDA.SelectCommand = cmd
myDA.Fill(ds, "tblCheckout")
rpt.SetDataSource(ds)
CheckoutReport.Show()
CheckoutReport.CrystalReportViewer1.ReportSource = rpt
cn.Close()
Catch Excep As Exception
MessageBox.Show(Excep.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
I HAVE A CODE HERE AND I WANT TO DISPLAY THE REPORT BETWEEN TO DATES TO OTHER FORM TO CLICK PREVIEW BUTTON, SO CAN YOU HELP ME WHEN I RUN THE MESSAGE APPEAR IS
object reference not set to an instance of an object
To answer your question on why you're getting a "object reference not set to an instance of an object" look at your SqlConnection. You declare it but never instantiate it so it's null/nothing when you reference it giving you that error. Your probably getting it on the cn.Open() line.
Change:
Dim cn As SqlConnection
to something like:
Dim cn As New SqlConnection("server=yourserver;database=yourdatabase;uid=sa=pwd=sa123")

Populating datagrid1.view with a SQL Server stored procedure

I got a stored procedure in SQL Server I created some inner joins and now how will I populate my datagrid using that stored procedure.
Here is my code that is not working
Dim cmd As New SqlCommand
Dim reader As SqlDataReader
cmd.CommandText = "OfficeEquipmentProfile"
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = sqlconn
sqlconn.Open()
sAdapter = New SqlDataAdapter(cmd)
sBuilder = New SqlCommandBuilder(sAdapter)
sDs = New DataSet
sAdapter.Fill(sDs, "tblOfficeEquipmentProfile")
sAdapter.Fill(sDs, "tblDepartment")
sAdapter.Fill(sDs, "tblLocation")
sAdapter.Fill(sDs, "tblOfficeEquipmentCategory")
sAdapter.Fill(sDs, "tblApplication")
sAdapter.Fill(sDs, "tblApplicationLicense")
sAdapter.Fill(sDs, "tblEquipmentApplication")
sAdapter.Fill(sDs, "tblOfficeEquipmentBrand")
sAdapter.Fill(sDs, "tblOfficeEquipmentModel")
sAdapter.Fill(sDs, "tblOfficeEquipmentServiceOrder")
sAdapter.Fill(sDs, "tblOfficeEquipmentPMplan")
sTable = sDs.Tables("tblOfficeEquipmentProfile")
sTable = sDs.Tables("tblDepartment")
sTable = sDs.Tables("tblLocation")
sTable = sDs.Tables("tblOfficeEquipmentCategory")
sTable = sDs.Tables("tblApplication")
sTable = sDs.Tables("tblApplicationLicense")
sTable = sDs.Tables("tblEquipmentApplication")
sTable = sDs.Tables("tblOfficeEquipmentBrand")
sTable = sDs.Tables("tblOfficeEquipmentServiceOrder")
sTable = sDs.Tables("tblOfficeEquipmentPMplan")
DataGrid1.DataSource = sDs.Tables("tblOfficeEquipmentProfile, tblDepartment, tblLocation, tblOfficeEquipmentCategory, tblApplication,tblApplicationLicense, tblEquipmentApplication, tblOfficeEquipmentBrand, tblOfficeEquipmentServiceOrder,tblEquipmentPMplan")
DataGrid1.ReadOnly = True
'Button1.Enabled = False
'DataGrid1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
reader = cmd.ExecuteReader()
sqlconn.Close()
I just want to display records from the database
As you are returning columns from different tables and not multiple tables then you just need this code.
Dim Command As SqlCommand = New SqlCommand()
Command.Connection = Connection
Command.CommandText = "OfficeEquipmentProfile"
Command.CommandType = CommandType.StoredProcedure
Dim sAdapter As SqlDataAdapter = New SqlDataAdapter(Command)
Dim DataSet As DataSet = New DataSet(Command.CommandText)
sAdapter.Fill(DataSet)
DataGrid1.DataSource = DataSet.Tables(0)
Try with this code
Dim cmd As New SqlCommand
cmd.CommandText = "OfficeEquipmentProfile"
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = sqlconn
sqlconn.Open()
sAdapter = New SqlDataAdapter(cmd)
sBuilder = New SqlCommandBuilder(sAdapter)
sDs = New DataSet
sAdapter.Fill(sDs)
DataGrid1.DataSource = sDs
The storedprocedure return one or more tables, but you don't need to call a Fill for each table. One is enough. Then assign the whole Dataset to the Datasource or, if you return more than one table and need a specific table
DataGrid1.DataSource = sDs
DataGrid1.DataMember = "tablename"
Try
If con.State = ConnectionState.Open Then con.Close()
con.Open()
Dim dset As New DataSet
Dim dbind As New BindingSource
Dim strquery As String
strquery = "SELECT prod_code, prod_no, prod_suffix, prod_lvl, customer, model, family, company_code, company_name, company_address, running_no, template_code FROM products_tbl WHERE template_code = 'n'and company_code = 'YTPL' and prod_no = '" & txt_product.Text & "' and prod_suffix = '" & txt_suffix.Text & "' and prod_lvl = '" & txt_level.Text & "'"
Dim adap As New SqlDataAdapter(strquery, con)
dset = New DataSet
adap.Fill(dset)
dbind.DataSource = dset.Tables(0)
dg.DataSource = dbind
Catch ex As Exception
MsgBox("Trace No 3: System Error or Data Error!" + Chr(13) + ex.Message + Chr(13) + "Please Contact Your System Administrator!", vbInformation, "Message")
End Try

How to call a query from MS Access 2010 (.accdb) within VB.Net 2010?

I have this code that populates a datagridview with data from ms access:
Dim con As New OleDbConnection
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim Sql As String
Sql = "SELECT * FROM myTable WHERE case_no=?"
Try
con.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source=Sample.accdb;Persist Security Info=True;Jet OLEDB:Database Password=dbadmin2010"
con.Open()
Dim cmd As OleDbCommand = New OleDbCommand(Sql, con)
da.SelectCommand = cmd
cmd.Parameters.AddWithValue("case_no", case_no)
Dim ds As DataSet = New DataSet
da.Fill(ds, "Case Info")
DataGridView1.DataSource = ds.Tables("Case Info")
Catch ex As Exception
MsgBox(ex.Message)
End Try
Now I had just finished creating a query from design view within MS Access itself, is there a way to call that query and retrieve the results to my datagridview?
Just use the query name and set the command type, for example, in addition to what you already have, you can use the following notes:
Try
con.ConnectionString = enter_connection_string_here
con.Open()
cmd.Connection = con
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "NameOfQuery"
da.SelectCommand = cmd
cmd.Parameters.AddWithValue("case_no", case_no)
da.Fill(ds, "Case Info")