Error during insert the XML file content into an access database - asp.net

in a web application, i want to insert the XML file content into an access database, i got the code (below), but there is an error with this line
xmlFile = XmlReader.Create("Product.xml", New XmlReaderSettings())
this error appear
please take a look at the code below and tell me how to modify to get the "product.xml" from the web application root folder. i tried "~/product.xml" and it did not work
Imports Microsoft.VisualBasic
Imports System.Xml
Imports System.Data.OleDb
Imports System.Data
Public Class Form1
Public Shared Sub mimi()
Dim connetionString As String
Dim connection As OleDbConnection
Dim command As OleDbCommand
Dim ds As New DataSet
Dim xmlFile As XmlReader
Dim sql As String
Dim adpter As New OleDbDataAdapter
Dim product_ID As Integer
Dim Product_Name As String
Dim product_Price As Double
connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= |datadirectory|CRF.mdb;Persist Security Info=True"
connection = New OleDbConnection(connetionString)
xmlFile = XmlReader.Create("Product.xml", New XmlReaderSettings())
ds.ReadXml(xmlFile)
Dim i As Integer
connection.Open()
For i = 0 To ds.Tables(0).Rows.Count - 1
product_ID = Convert.ToInt32(ds.Tables(0).Rows(i).Item(0))
Product_Name = ds.Tables(0).Rows(i).Item(1)
product_Price = Convert.ToDouble(ds.Tables(0).Rows(i).Item(2))
sql = "insert into Product values(" & product_ID & ",'" & Product_Name & "'," & product_Price & ")"
command = New OleDbCommand(sql, connection)
adpter.InsertCommand = command
adpter.InsertCommand.ExecuteNonQuery()
Next
connection.Close()
End Sub
End Class

try to use HttpContext.Current.Server.MapPath("~/Product.xml")

Related

Asp.net multible table access vb.net

and good day..
I have a code of asp.net in vb.net and work fine for one table but i need to add multible table of( master and dr ) to code the data is the sum from the two table but its different. can anyone help me..thanks advance for all
Private Sub getData(ByVal user As String)
Dim dt As New DataTable()
Dim connectionString As String
Dim connection As OleDbConnection
connectionString = ConfigurationManager.ConnectionStrings("Data_For_netConnectionString").ToString
connection = New OleDbConnection(connectionString)
connection.Open()
Dim sqlCmd As New OleDbCommand("SELECT * from master WHERE UserID = #user", connection)
sqlCmd.CommandText = _
"SELECT name, manistry, university, send, card FROM master "
Dim sqlDa As New OleDbDataAdapter(sqlCmd)
sqlCmd.Parameters.AddWithValue("#user", user)
sqlDa.Fill(dt)
If dt.Rows.Count > 0 Then
TextBox1.Text = dt.Rows(0)("manistry").ToString
TextBox2.Text = dt.Rows(0)("university").ToString
TextBox4.Text = dt.Rows(0)("send").ToString
TextBox21.Text = dt.Rows(0)("card").ToString
name.Text = dt.Rows(0)("name").ToString
End If
connection.Close()
End Sub

excel to access using vb.net

net and I'm looking for a way to import excel sheet to ms access and I found this site(http://www.mikesdotnetting.com/Article/79/import-data-from-excel-to-access-with-asp-net), then I use the code with some modification on it and here's the code:
Imports System.Data.OleDb
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim provider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
Dim dataFile As String = "C:\Users\user\Documents\Visual Studio 2010\Projects\System3.0\System3.0.accdb"
Dim Access As String = provider & dataFile
Dim Excel As String = "C:\Users\user\Desktop\Book1.xlsx"
Dim connect As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & Excel & ";" & "Extended Properties=Excel 12.0 Xml; HRD = YES"
Using conn As New OleDbConnection(connect)
Using cmd As New OleDbCommand()
cmd.Connection = conn
cmd.CommandText = "INSERT INTO [MS Access;Database=" & Access & "].[Student] SELECT * FROM [Sheet1$]"
conn.Open()
cmd.ExecuteNonQuery()
End Using
End Using
End Sub
End Class
Problem is that I got this error
An unhandled exception of type 'System.Data.OleDb.OleDbException'
occurred in System.Data.dll
Additional information: Invalid argument." in cmd.ExecuteNonQuery()
Please help me find the problem...thanks in advance...

ASPX page to open excel file in local c drive - permission error

I have this sub in ASPX page. code behind is VB.net.
This sub will open an excel file and I would like to fill a datatable.
but I am getting a permission error. how can I solve this?
Thank you
The error I get is
Exception Details: System.Data.OleDb.OleDbException: The Microsoft
Access database engine cannot open or write to the file ''. It is
already opened exclusively by another user, or you need permission to
view and write its data.
Private Sub ProcessFile(sFileName As String)
Dim sConnectionString As String
sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & sFileName & ";Extended Properties = ""Excel 12.0 Xml;HDR=NO"""
Dim OLEConn As New OleDbConnection(sConnectionString)
Dim sSQL As String
sSQL = "Select * from [Sheet1$]"
Dim cmd = New OleDbCommand(sSQL, OLEConn)
Dim dt As New DataTable
Dim OLEAdapter As New OleDbDataAdapter(cmd)
OLEAdapter.Fill(dt)
End Sub

dropdownlist is not declared. It may be inaccessible due to its protection level

As you can see from the commented out code, I'm trying to get the model dropdown be affected by + selCurrentManuf.Text.
I get this error
'selCurrentManuf' is not declared. It may be inaccessible due to its protection level.
How can this be solved?
I can access the drop down in another part of the page like this..
Dim sc1_currentmanuf As String = CType(e.Item.FindControl("selCurrentManuf"), DropDownList).Text
However in the function i am trying to use selCurrentManuf does not have access to e
Dim sc1_currentmanuf As String = CType(dlContacts.Items(0).FindControl("selCurrentManuf"), DropDownList).Text
Dim myQuery As String = "SELECT * FROM c5_model where c5_manufid = " + sc1_currentmanuf
Right click on your .aspx page, and select the command Convert To Web Application.
Then you'll be able to write:
Dim myQuery As String =
String.Format("SELECT * FROM c5_model WHERE c5_manuf = '{0}'",
selCurrentManuf.SelectedItem.Text )
I'm assuming your functions are inside a class in your App_Code or another dll and not on the code behind of the page.
If so do this instead:
I'm assuming you have something like this on your asp page code behind:
Protected Sub selCurrentManuf_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
GetCurrentModel(selCurrentManuf.Text)
End Sub
Change Your GetCurrentModel Code To:
Function GetCurrentModel(Byval c5_manuf as String) As DataSet
Dim mySession = System.Web.HttpContext.Current.Session
Dim myQuery As String = "SELECT * FROM c5_model " 'where c5_manuf = " + + c5_manuf
Dim myConnection As New MySqlConnection(mySession("localConn"))
myConnection.Open()
Dim myCommand As New MySqlCommand(myQuery, myConnection)
Dim myDataAdapter = New MySqlDataAdapter(myCommand)
Dim myDataset As New DataSet
myDataAdapter.Fill(myDataset, "c5_model")
Dim dr As DataRow = myDataset.Tables(0).NewRow
myDataset.Tables(0).Rows.Add(dr)
GetCurrentModel = myDataset
myConnection.Close()
End Function

Accessing values from partial classes

Please help! I'm still learning so this might be completely wrong. I have built a multi-user web application for internal purposes. I have split my code up to make it more manageable and have run into some problems. I suspect that I have done something very silly!
I have a class in a separate file (quoteStatus.vb) in App_Code here it is:
Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Imports System.Data
Public Class quoteStatusHelper
Public Shared QuoteStatus As String
Public Shared Sub GetQuoteStatus(ByVal QuoteID As String)
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Try
con.ConnectionString = ConfigurationManager.AppSettings("quotingSystemConnectionString")
con.Open()
cmd.Connection = con
cmd.CommandText = "SELECT Status FROM Quote WHERE QuoteID =" & QuoteID & ";"
Dim lrd As SqlDataReader = cmd.ExecuteReader()
While lrd.Read()
QuoteStatus = lrd("Status")
End While
Catch ex As Exception
Finally
con.Close()
End Try
End Sub
End Class
I use this to fire the code from another file:
quoteStatusHelper.GetQuoteStatus(QuoteID)
Then grab the result with
Dim returnedStatus = quoteStatusHelper.QuoteStatus
This seems really wrong, as the value seems to be held in the partial class after the first run. So if I have two users accessing the class the public shared dim is changed for both users! There must be a better way to do this. I have looked everywhere and I'm more confused now than when I started off...
How can I make QuoteStatus in the partial class unique to every user and access it from my code. I cant figure it out :( Please help
Many thanks in advance.
The problem is that your QuoteStatus variable is shared/static. You don't need to do that here. Declaring it as Public will make it visible to other classes. Remove Shared from the method and the QuoteStaus variable, and all should be well.
Once you remove Shared it will be an instance class, which means that you'll need to initialize it in your code:
Dim statusHelper As New quoteStatusHelper()
statusHelper.GetQuoteStatus("QuoteID")
EDIT
Taking a closer look at your code, it looks like you could make GetQuoteStatus a function that returns the QuoteStatus as a string, as #Claudio Redi suggested.
Public Function GetQuoteStatus(ByVal QuoteID As String) As String
Dim quoteStatus As String
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Try
con.ConnectionString = ConfigurationManager.AppSettings("quotingSystemConnectionString")
con.Open()
cmd.Connection = con
cmd.CommandText = "SELECT Status FROM Quote WHERE QuoteID =" & QuoteID & ";"
Dim lrd As SqlDataReader = cmd.ExecuteReader()
While lrd.Read()
quoteStatus = lrd("Status")
End While
Catch ex As Exception
Finally
con.Close()
End Try
Return quoteStatus
End Function
By making GetQuoteStatus a function, you'll be able to get the value like this:
Dim statusHelper As New quoteStatusHelper()
Dim quoteStatus As String = statusHelper.GetQuoteStatus("QuoteID")
An option would be using a local variable on your method for quoteStatus and using it as return value of GetQuoteStatus. So you wouldn't use anymore the shared member QuoteStatus
EDIT
Added a function example. You should remove your shared member QuoteStatus from your class. I don't use VB on my day to day work so I hope I didn't make any stupid syntax mistake
Public Shared Function GetQuoteStatus(ByVal QuoteID As String) as String
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim quoteStatus As String
Try
con.ConnectionString = ConfigurationManager.AppSettings("quotingSystemConnectionString")
con.Open()
cmd.Connection = con
cmd.CommandText = "SELECT Status FROM Quote WHERE QuoteID =" & QuoteID & ";"
Dim lrd As SqlDataReader = cmd.ExecuteReader()
While lrd.Read()
quoteStatus = lrd("Status")
End While
Catch ex As Exception
Finally
con.Close()
End Try
return quoteStatus
End Function

Resources