How to import Excel File in Database through asp.net
I have a class in inherit database class into that. Like this
Conection String to Database.
Public Class clsmemberContribution : Inherits cls_Database
Public Function InsertExcelDataIntoDB() As Boolean
Dim sSQL As String
Dim rtn As Boolean
Dim rs As SqlDataReader = Nothing
Try
InsertExcelDataIntoDB = True
sSQL = "write store procedure or sql here"
' 'execute the sql statement an process the results
rtn = GetData(rs, sSQL)
If rtn Then
If rs.Read() Then
memberFeeID = gFixADONull(rs(0).GetType(), rs(0) & "")
End If
Else
ErrMessage = ErrMessage
rtn = False
End If
Return rtn
Catch e As Exception
ErrMessage = e.Message
Return False
Finally
If Not rs Is Nothing Then
rs.Close()
End If
CloseDatabase()
End Try
End Function
End Class
HTML
<asp:FileUpload ID="FileUploadExcel" runat="server" />
<asp:Button ID="btnImport" runat="server" Text="Upload" OnClick="btnImport_click" />
Code
Protected Sub btnImport_click(ByVal sender As Object, ByVal e As System.EventArgs)
If FileUploadExcel.HasFile = False Then
lblMessage.Text = "<div Class="" alert alert-Error"">There Is no file For upload.</div>"
Exit Sub
ElseIf FileUploadExcel.HasFile = True Then
Dim FileExtention As String = System.IO.Path.GetExtension(FileUploadExcel.FileName)
If FileExtention = ".xlsx" Or FileExtention = ".xls" Then
' Response.Write("It's a PDF File.")
lblMessage.Text = "Ok"
Exit Sub
Else
'Response.Write("It's NOT PDF File.")
lblMessage.Text = "Incorrect"
Exit Sub
End If
End If
End Sub
Excel File has two Column
ID Name
1 aaa
2 cccc
3 xxxxx
I need to import excel file into database please guide