excel to access using vb.net - asp.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...

Related

Must declare the scalar variable "#User_Name"

Referring to my last question: "The connection was not closed. The connection's current state is open". I am trying to do a registration form using vb.net with ASP.NET. After having solve an issue concerning 'close connection'. I am having problem to insert data to my database. When I try to validate the data it gives me an error message:
Must declare the scalar variable "#User_Name"
Can someone help me to debug this? Thanks
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Partial Class Register2
Inherits System.Web.UI.Page
'declaring connection string and command
'here we are extracting connection string from web.config file
Private con As New SqlConnection(ConfigurationManager.ConnectionStrings("MauriAuctions").ToString())
Private cmd As New SqlCommand()
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Try
cmd.Connection = con
'assigning connection to command
cmd.CommandType = CommandType.Text
'representing type of command
'cmd.CommandText = "INSERT INTO UserDetails (User_Name,Fname,Lname,Email,Password,Gender,Dob,Mobile,Address) values
' (#User_Name,#Fname,#Lname,#Email,#Password,#Gender,#Dob,#Mobile,#Address)";
cmd.CommandText = "INSERT INTO tbl_user (User_Name, Fname, Lname, Email, Pwd, Street, Town, City, Tel) values(#User_Name,#Fname,#Lname,#Email,#Pwd,#Street,#Town,#City,#Tel)"
'adding parameters with value
cmd.Parameters.AddWithValue("#User_Name", txtUser_Name.Text.ToString())
cmd.Parameters.AddWithValue("#Fname", txtFirstName.Text.ToString())
cmd.Parameters.AddWithValue("#Lname", txtLastName.Text.ToString())
cmd.Parameters.AddWithValue("#Email", txtEmail.Text.ToString())
cmd.Parameters.AddWithValue("#Pwd", txtPassword.Text.ToString())
cmd.Parameters.AddWithValue("#Street", txtStreet.Text.ToString())
cmd.Parameters.AddWithValue("#Town", txtTown.Text.ToString())
cmd.Parameters.AddWithValue("#City", txtCity.Text.ToString())
cmd.Parameters.AddWithValue("#Tel", txtTel.Text.ToString())
cmd.Parameters.Clear()
con.Open()
'opening connection
cmd.ExecuteNonQuery()
'executing query
con.Close()
'closing connection
lblMsg.Text = "Registered Successfully.."
Catch ex As Exception
lblMsg.Text = ex.Message.ToString()
Finally
con.Close()
'closing connection
End Try
End Sub
Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
'refreshing/reloading page to clear all the controls
Page.Response.Redirect(Page.Request.Url.ToString(), True)
End Sub
End Class
You're clearing parameters after adding them. Try putting cmd.Parameters.Clear() before first .AddWithValue, not after the last one.

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

Error during insert the XML file content into an access database

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")

connect to SQL using asp.net

anyone can help me to connect to SQL server through vb.net using asp.net webform.. I have the database name Users and i want to use the database for the login page.. please help me..
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ConnectionString As String
ConnectionString = ConfigurationSettings.AppSettings("ConnectionString")
Dim con As New SqlConnection(ConnectionString)
Dim cmd As New SqlCommand("Select UserId, Pwd from Users", con)
con.Open()
Dim myreader As SqlDataReader
myreader = cmd.ExecuteReader()
While myreader.Read()
If TxtUserId.Text = myreader("UserId").ToString().Trim()
AndAlso TxtPwd.Text = myreader("Pwd").ToString().Trim() Then
Session("UserId") = TxtUserId.Text
Response.Redirect("UserMyProfile.aspx")
Else
lblMsg.Visible = True
lblMsg.Text = "Inavalid UserId/Password"
End If
End While
con.Close()
End Sub
There's no shortage of tutorials on the web for this, but a good starting point is here.
EDIT: Based on your comments above, it sounds like you're not importing the Namespace you need for the ADO.NET data objects. Try adding this to the class file:
Imports System.Data.SqlClient

ASP.net using label.Text in select query

I have to run a SQL query using a text value in a label and then run that query and bind data to a gridview. Here's my code in VB.net
Dim myConnection As SqlConnection = New SqlConnection
Dim ad As New SqlDataAdapter
Dim details As New DataSet
Dim detailcmd As New SqlCommand("select student_name,student_id from students where student_name = '" + snamelabel.Text + "'", myConnection)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ad.SelectCommand = detailcmd
myConnection.ConnectionString = "Data Source=USER-PC\SQLEXPRESS;Initial Catalog=students;Integrated Security=True"
myConnection.Open()
ad.Fill(details, "Details")
myConnection.Close()
DetailGridView.DataSource = details
DetailGridView.DataBind()
End Sub
I get the following error message for the SqlCommand --->
Object reference not set to an instance of an object.
Is the data binding for grid view correct?
Any ideas?
1- This line will cause sql Injection in the future.
Dim detailcmd As New SqlCommand(
"select student_name,student_id from students where student_name = '"
+ snamelabel.Text + "'", myConnection)
2- No Need to open/close the connection when use data adapter..
3- I think the error because you are initializing the Command in the class try move it to page load event.

Resources