I want to create a feature within my program that when i load in an excel file, it counts how many columns in the spreadsheet and dynamically creates a dropdown for each and every column, to select what header it should be, and bind it to the GridView .Data from SQL server will populate the dropdowns. Some problems i have run into is, how do i have a button where it asks for the file location? i have tried this way to load in a hardcoded location, but it doesnt work.
Sub SubmitBtn_Excel(Sender As Object, E As EventArgs)
Try
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim dataSet As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
Dim path As String = "C:\\spreadsheet.xlsx"
MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;")
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
dataSet = New System.Data.DataSet
MyCommand.Fill(dataSet)
GridView1.DataSource = dataSet.Tables(0)
MyConnection.Close()
Catch ex As Exception
End Try
End Sub
I also have a method to call on the database with the table i have created to populate the data, should i put this method all in one method with the excel call?? considering that the dropdown id was dynamically created, it doesnt know how to find it because its not id'd in the HTML
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not Page.IsPostBack Then
FillDeptDropdownList()
End If
End Sub
Protected Sub FillDeptDropdownList()
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("MyDbCon").ConnectionString)
Dim cmd As New SqlCommand("Select * from demofeepay.dbo.catagories", con)
Dim adp As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
adp.Fill(dt)
ddlDynamic.DataSource = dt
ddlDynamic.DataTextField = "catagory"
ddlDynamic.DataValueField = "ID"
ddlDynamic.DataBind()
ddlDynamic.Items.Insert(0, "Select Catagory")
End Sub
This is the method i have for creating the dropdown
Sub SubmitBtn_Excel(Sender As Object, E As EventArgs)
Try
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim dataSet As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
Dim path As String = "C:\\Users\\John\\Documents\\John\\EPP\\WorldNet_Notes\\CHRISTIAN_BROTHERS_HIGH_SCHOOL.xlsx"
MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;")
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
dataSet = New System.Data.DataSet
MyCommand.Fill(dataSet)
GridView1.DataSource = dataSet.Tables(0)
MyConnection.Close()
Catch ex As Exception
End Try
End Sub
This is the HTML
<form id="form1" runat="server" style="overflow-x:hidden; width:100%">
<div>
<asp:button id="butOK" text="Add Dropdowns" onclick="SubmitBtn_Click" runat="server"/>
<asp:button id="BtnAddExcel" text="Add Spreadsheet" OnClick="SubmitBtn_Excel" runat="server"/>
</div>
<div id ="ddlDynamic">
<!-- Dynamic Dropdowns -->
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</div>
</form>
I am aware that this is all over the place, as you can see im a bit lost. I dont really want anybody to spoon fed me the answer, im just unsure how to structure it, or could anybody point me in the right direction to find out more. New to vb. Thanks in advance :-)
Aucun commentaire:
Enregistrer un commentaire