clearchannel
asked on
Convert XLS to dataset and update database
I am trying to upload an XLS spreadsheet put it into a dataset and then loop through the dataset and update my database.
At the moment I can populate the Gridview1 with the xls BUT it gets all the EMPTY rows from the xlas as well as the data rows, which is no good!
Another issue is that i keep getting System.IO.IOException errors due to the file bing in use all the time when the Subroutine fails.
And the final issue is with the id/RosterID field. If there is a new entry in the xls spreadsheet there is no id field and so I default it to 0 unless there is a value for id, but there is an issue here in that it says it's null but I am not understanding why
At the moment I can populate the Gridview1 with the xls BUT it gets all the EMPTY rows from the xlas as well as the data rows, which is no good!
Another issue is that i keep getting System.IO.IOException errors due to the file bing in use all the time when the Subroutine fails.
And the final issue is with the id/RosterID field. If there is a new entry in the xls spreadsheet there is no id field and so I default it to 0 unless there is a value for id, but there is an issue here in that it says it's null but I am not understanding why
If FileUpload1.HasFile Then
FileUpload1.PostedFile.SaveAs(Server.MapPath("/roster/temp/rosterimport.xls"))
'View in grid
'Dim objDataAdapter As New OleDbDataAdapter()
'objDataAdapter.SelectCommand = ExcelConnection()
'Dim objDataSet As New DataSet()
'objDataAdapter.Fill(objDataSet)
'GridView1.DataSource = objDataSet.Tables(0).DefaultView
'GridView1.DataBind()
'GridView1.Visible = True
'objDataAdapter.Dispose()
'objDataSet.Dispose()
Dim objDataAdapter As New OleDbDataAdapter()
objDataAdapter.SelectCommand = ExcelConnection()
Dim dsRosterUpdate As New DataSet()
objDataAdapter.Fill(dsRosterUpdate)
Dim i As Integer = 0
While i < dsRosterUpdate.Tables(0).Rows.Count
Dim RosterID As Integer = dsRosterUpdate.Tables(0).Rows(i)("id")
Dim FirstName As String = Convert.ToString(dsRosterUpdate.Tables(0).Rows(i)("first_name"))
Dim LastName As String = Convert.ToString(dsRosterUpdate.Tables(0).Rows(i)("last_name"))
Dim JobTitle As String = Convert.ToString(dsRosterUpdate.Tables(0).Rows(i)("job_title"))
Dim Department As String = Convert.ToString(dsRosterUpdate.Tables(0).Rows(i)("department"))
Dim Market As String = Convert.ToString(dsRosterUpdate.Tables(0).Rows(i)("market"))
Dim BusinessUnit As String = Convert.ToString(dsRosterUpdate.Tables(0).Rows(i)("business_unit"))
Dim Division As String = Convert.ToString(dsRosterUpdate.Tables(0).Rows(i)("division"))
Dim Email As String = Convert.ToString(dsRosterUpdate.Tables(0).Rows(i)("email"))
Dim Tel As String = Convert.ToString(dsRosterUpdate.Tables(0).Rows(i)("tel"))
Dim DirectTel As String = Convert.ToString(dsRosterUpdate.Tables(0).Rows(i)("direct_tel"))
Dim Fax As String = Convert.ToString(dsRosterUpdate.Tables(0).Rows(i)("fax"))
Dim Mobile As String = Convert.ToString(dsRosterUpdate.Tables(0).Rows(i)("mobile"))
Dim Address As String = Convert.ToString(dsRosterUpdate.Tables(0).Rows(i)("address"))
Dim Assistant As String = Convert.ToString(dsRosterUpdate.Tables(0).Rows(i)("assistant"))
Dim AssistantTel As String = Convert.ToString(dsRosterUpdate.Tables(0).Rows(i)("assistant_tel"))
Dim Languages As String = Convert.ToString(dsRosterUpdate.Tables(0).Rows(i)("languages"))
Dim OtherInfo As String = Convert.ToString(dsRosterUpdate.Tables(0).Rows(i)("other_info"))
ImportLabel.Text &= RosterID & " " & LastName & " " & FirstName & " " & JobTitle & " " & Department & " " & Market & " " & BusinessUnit & " " & Division & " " & Email & " " & Tel & " " & DirectTel & " " & Fax & " " & Mobile & "<br>" & Address & " " & Assistant & " " & AssistantTel & " " & Languages & " " & OtherInfo & "<hr>"
If RosterID <> 0 Then
ImportLabel.Text &= "<font color='Blue'>Roster Update!</font><br>"
'UpdateRoster(RosterID, LastName, FirstName, JobTitle, Department, Market, BusinessUnit, Division, Email, Tel, DirectTel, Fax, Mobile, Address, Assistant, AssistantTel, Languages, OtherInfo)
Else
ImportLabel.Text &= "<font color='green'>Roster Insert!</font><br>"
'AddToRoster(LastName, FirstName, JobTitle, Department, Market, BusinessUnit, Division, Email, Tel, DirectTel, Fax, Mobile, Address, Assistant, AssistantTel, Languages, OtherInfo)
End If
i += 1
End While
End If
End Sub
Protected Function ExcelConnection() As OleDbCommand
Dim xConnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("/roster/temp/rosterimport.xls") & ";Extended Properties=Excel 8.0;"
Dim objXConn As New OleDbConnection(xConnStr)
objXConn.Open()
Dim objCommand As New OleDbCommand("SELECT * FROM [Roster$]", objXConn)
Return objCommand
End Function
This should work
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ImportExcellIntoTable()
End Sub
Public Sub ImportExcellIntoTable()
Dim mConn As New OleDb.OleDbConnection("PRO VIDER=Micr osoft.Jet. OLEDB.4.0; Data Source = C:\Test.mdb")
Dim mDataAdapter As New OleDb.OleDbDataAdapter
Dim mCommandBuilder As OleDb.OleDbCommandBuilder
Dim ds As New DataSet
ds.Tables.Add(New DataTable("dtbl"))
ds.Tables("dtbl").Columns. Add(New DataColumn("Id", GetType(Integer)))
ds.Tables("dtbl").Columns. Add(New DataColumn("Name", GetType(String)))
ds.Tables("dtbl").Columns. Add(New DataColumn("Test", GetType(String)))
'read the excell sheet into a dataset
Dim cnn As New OleDb.OleDbConnection("Pro vider=Micr osoft.Jet. OLEDB.4.0; Data Source=C:\test.xls;Extende d Properties=""Excel 8.0;HDR=Yes;""")
Dim da As New OleDb.OleDbDataAdapter("Se lect * from [MySheet$]", cnn)
da.Fill(ds, "dtbl")
ds.Tables( "dtbl").AcceptChanges()
Try
mDataAdapter.SelectCommand = New OleDb.OleDbCommand("SELECT * FROM AccessTargetTable;", mConn)
mCommandBuilder = New OleDb.OleDbCommandBuilder( mDataAdapt er)
mDataAdapter.FillSchema(ds , SchemaType.Mapped, "dtbl")
For Each oDatarow As DataRow In ds.Tables("dtbl").Rows
oDatarow.SetAdded()
Next
mDataAdapter.Update(ds.Tab les("dtbl" ))
ds.AcceptChanges()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
vbturbo
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ImportExcellIntoTable()
End Sub
Public Sub ImportExcellIntoTable()
Dim mConn As New OleDb.OleDbConnection("PRO
Dim mDataAdapter As New OleDb.OleDbDataAdapter
Dim mCommandBuilder As OleDb.OleDbCommandBuilder
Dim ds As New DataSet
ds.Tables.Add(New DataTable("dtbl"))
ds.Tables("dtbl").Columns.
ds.Tables("dtbl").Columns.
ds.Tables("dtbl").Columns.
'read the excell sheet into a dataset
Dim cnn As New OleDb.OleDbConnection("Pro
Dim da As New OleDb.OleDbDataAdapter("Se
da.Fill(ds, "dtbl")
ds.Tables( "dtbl").AcceptChanges()
Try
mDataAdapter.SelectCommand
mCommandBuilder = New OleDb.OleDbCommandBuilder(
mDataAdapter.FillSchema(ds
For Each oDatarow As DataRow In ds.Tables("dtbl").Rows
oDatarow.SetAdded()
Next
mDataAdapter.Update(ds.Tab
ds.AcceptChanges()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
vbturbo
ASKER
Will your code actually not import the empty XLS rows?
I need seperate functions for update and insertion as there may be new employees and there maybe updates.
I need seperate functions for update and insertion as there may be new employees and there maybe updates.
ASKER
vbturbo: please could you answer my question?
ASKER
Has anyone got any solutions on how to not insert loads of empty rows at the bottom of the XLS file?
Thanks
Thanks
Hi
Access
Id|name|test
Excell first row have these column names =Id|name|test
Id|name|test
1 aa bb
2 aa bb
3 aa bb
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ImportExcellIntoTable()
End Sub
Public Sub ImportExcellIntoTable()
Dim mConn As New OleDb.OleDbConnection("PRO VIDER=Micr osoft.Jet. OLEDB.4.0; Data Source = C:\Test.mdb")
Dim mDataAdapter As New OleDb.OleDbDataAdapter
Dim mCommandBuilder As OleDb.OleDbCommandBuilder
Dim ds As New DataSet
ds.Tables.Add(New DataTable("dtbl"))
ds.Tables("dtbl").Columns. Add(New DataColumn("Id", GetType(Integer)))
ds.Tables("dtbl").Columns. Add(New DataColumn("Name", GetType(String)))
ds.Tables("dtbl").Columns. Add(New DataColumn("Test", GetType(String)))
'read the excell sheet into a dataset
Dim cnn As New OleDb.OleDbConnection("Pro vider=Micr osoft.Jet. OLEDB.4.0; Data Source=C:\test.xls;Extende d Properties=""Excel 8.0;HDR=Yes;""")
Dim da As New OleDb.OleDbDataAdapter("Se lect * from [MySheet$]", cnn)
da.Fill(ds, "dtbl")
ds.Tables( "dtbl").AcceptChanges()
Try
mDataAdapter.SelectCommand = New OleDb.OleDbCommand("SELECT * FROM AccessTargetTable;", mConn)
mCommandBuilder = New OleDb.OleDbCommandBuilder( mDataAdapt er)
mDataAdapter.FillSchema(ds , SchemaType.Mapped, "dtbl")
For Each oDatarow As DataRow In ds.Tables("dtbl").Rows
oDatarow.SetAdded()
Next
mDataAdapter.Update(ds.Tab les("dtbl" ))
ds.AcceptChanges()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Then if you want to check for emty rows then iterate through the dataset table and pick a column where you
want to examine if the cell value is empty.
For Each oDatarow As DataRow In ds.Tables("dtbl").Rows
if oDatarow(0) = "" then
oDatarow.Delete()
endif
Next
then delete that row!
vbturbo
Access
Id|name|test
Excell first row have these column names =Id|name|test
Id|name|test
1 aa bb
2 aa bb
3 aa bb
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ImportExcellIntoTable()
End Sub
Public Sub ImportExcellIntoTable()
Dim mConn As New OleDb.OleDbConnection("PRO
Dim mDataAdapter As New OleDb.OleDbDataAdapter
Dim mCommandBuilder As OleDb.OleDbCommandBuilder
Dim ds As New DataSet
ds.Tables.Add(New DataTable("dtbl"))
ds.Tables("dtbl").Columns.
ds.Tables("dtbl").Columns.
ds.Tables("dtbl").Columns.
'read the excell sheet into a dataset
Dim cnn As New OleDb.OleDbConnection("Pro
Dim da As New OleDb.OleDbDataAdapter("Se
da.Fill(ds, "dtbl")
ds.Tables( "dtbl").AcceptChanges()
Try
mDataAdapter.SelectCommand
mCommandBuilder = New OleDb.OleDbCommandBuilder(
mDataAdapter.FillSchema(ds
For Each oDatarow As DataRow In ds.Tables("dtbl").Rows
oDatarow.SetAdded()
Next
mDataAdapter.Update(ds.Tab
ds.AcceptChanges()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Then if you want to check for emty rows then iterate through the dataset table and pick a column where you
want to examine if the cell value is empty.
For Each oDatarow As DataRow In ds.Tables("dtbl").Rows
if oDatarow(0) = "" then
oDatarow.Delete()
endif
Next
then delete that row!
vbturbo
ASKER
Cool I had a feeling the only way to check for empty rows was to check for an empty column value.
Will give this a try when I get round to it and let you know how it goes. Thanks
Will give this a try when I get round to it and let you know how it goes. Thanks
ASKER
Sorry, I have not had chance to try the code out yet due to other work.
I will try and test the code in the enxt week :)
I will try and test the code in the enxt week :)
ASKER
Still not got round to this work, due to having to work on other code.
I will reply properly once I have tried the method listed above :)
Sorry for the delay.
I will reply properly once I have tried the method listed above :)
Sorry for the delay.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
set the HDR=NO ' then you dont get the header data
after the adapter has read the xls data into the table then tbl.rows(0).delete()
/vbturbo
after the adapter has read the xls data into the table then tbl.rows(0).delete()
/vbturbo
clearchannel
You could a least look like you bothered !
Eighter reject the answer or accept it , you know! someone took his time and replied your question and provided you with a solution.
/Jens
You could a least look like you bothered !
Eighter reject the answer or accept it , you know! someone took his time and replied your question and provided you with a solution.
/Jens
ASKER
Jens,
I did try to implement your solution with my code but I could not get it work with the application I am working. Also you were busy and I could not get an answer soI had to go look elsewhere to find a solution that I could use as I required when I required it. Non of my code contains any of your solutions. I found other ways round my issues.
With regards to the header and null rows I added in an extra column into the Excel file and checked for certain values to get the desires result.
Sorry you feel agrieved. I can't share the points between myself as the question poster and you, otherwise I would :)
Simon
I did try to implement your solution with my code but I could not get it work with the application I am working. Also you were busy and I could not get an answer soI had to go look elsewhere to find a solution that I could use as I required when I required it. Non of my code contains any of your solutions. I found other ways round my issues.
With regards to the header and null rows I added in an extra column into the Excel file and checked for certain values to get the desires result.
Sorry you feel agrieved. I can't share the points between myself as the question poster and you, otherwise I would :)
Simon
Hi
well your initial questions was
<I am trying to upload an XLS spreadsheet put it into a dataset and then loop through the dataset and update my <database.
I provided you with a solution that does exactly that.
Did you even test my last provided solution ?
<How do I loop through the dataset created form the xlas file and call the function to either insert, update or <delete as in my code below? the READ method is causing an issue on the while loop?
Regarding your upload screen shot
i replied:
set the HDR=NO '
then that way you dont get the header data
after the adapter has read the xls data into the table then tbl.rows(0).delete()
but your code does not reflect this on the fill.
Dim xConnStr As String = "Provider=Microsoft.Jet.OL EDB.4.0;Da ta Source=" & Server.MapPath("/roster/te mp/rosteri mport.xls" ) & ";Extended Properties=Excel 8.0;"
http://www.connectionstrings.com/excel
Excel file without header row
Data Source =c:\myExcelFile.xls;HDR=no ;Format=xl s;
Secondly :
You will not get any updating against your database table if you do not set all of
your imported rows (from excel) to added
For Each oDatarow As DataRow In ds.Tables("dtbl").Rows
oDatarow.SetAdded()
Next
You will need set your rows state to added or your adapter will not reconize anything to update.
This your code does neighter reflect.
http://msdn.microsoft.com/en-us/library/ww3k31w0(VS.71).aspx
i recommend you to test my last provided solution (This works)
========================== ========== ========== ========== ========== ===
Computer101:
Please refund clearchannel his point
Its very hard for me to guess what his is looking for, since he is not elaborating or follow up on his thread
and it seems like he wants me to write he's entire solution from pure speculation
/vbturbo
well your initial questions was
<I am trying to upload an XLS spreadsheet put it into a dataset and then loop through the dataset and update my <database.
I provided you with a solution that does exactly that.
Did you even test my last provided solution ?
<How do I loop through the dataset created form the xlas file and call the function to either insert, update or <delete as in my code below? the READ method is causing an issue on the while loop?
Regarding your upload screen shot
i replied:
set the HDR=NO '
then that way you dont get the header data
after the adapter has read the xls data into the table then tbl.rows(0).delete()
but your code does not reflect this on the fill.
Dim xConnStr As String = "Provider=Microsoft.Jet.OL
http://www.connectionstrings.com/excel
Excel file without header row
Data Source =c:\myExcelFile.xls;HDR=no
Secondly :
You will not get any updating against your database table if you do not set all of
your imported rows (from excel) to added
For Each oDatarow As DataRow In ds.Tables("dtbl").Rows
oDatarow.SetAdded()
Next
You will need set your rows state to added or your adapter will not reconize anything to update.
This your code does neighter reflect.
http://msdn.microsoft.com/en-us/library/ww3k31w0(VS.71).aspx
i recommend you to test my last provided solution (This works)
==========================
Computer101:
Please refund clearchannel his point
Its very hard for me to guess what his is looking for, since he is not elaborating or follow up on his thread
and it seems like he wants me to write he's entire solution from pure speculation
/vbturbo
ASKER