Почему мой выпадающий список не работает?
Я пытаюсь прочитать данные из листа excel для моего выпадающего списка с помощью OleDB reader, но это не работает. Я пытаюсь объединить запрос для insert и select, но он просто работает только на INSERT, а для SELECT-нет. Есть ли какое-нибудь решение моей проблемы?
Что я уже пробовал:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type="text/css"> .auto-style2{ width:103px; text-align:right; } </style> </head> <body> <form id="form1" runat="server"> <div> <table class="auto-style1"> <tr> <td class="auto-style2">UserNTID:</td> <td> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </td> </tr> <tr> <td class="auto-style2">SerialNumber:</td> <td> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </td> </tr> <tr> <td class="auto-style2">Model:</td> <td> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> </td> </tr> <tr> <td class="auto-style2">Department:</td> <td> <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> </td> </tr> <tr> <td class="auto-style2"> <asp:Label ID="DropDownList1" runat="server" Text="Country"></asp:Label> </td> <td> <asp:DropDownList runat="server" AutoPostBack="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged"> <asp:ListItem Text="Select Country" Value="select" Selected="True"></asp:ListItem> </asp:DropDownList> </td> </tr> </table> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.OleDb; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string ConStr = ""; //getting the path of the file string path = Server.MapPath("Book1.xlsx"); //connection string for that file which extantion is .xlsx ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;ReadOnly=False;HDR=Yes;\""; //making query string query = "INSERT INTO [Sheet1$] ([UserNTID], [SerialNumber], [Model], [Department]) VALUES('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "')"; query += "SELECT * FROM [Sheet1$]([Country]) VALUES('"+ DropDownList1 + "')"; //Providing connection OleDbConnection conn = new OleDbConnection(ConStr); //checking that connection state is closed or not if closed the //open the connection if (conn.State == ConnectionState.Closed) { conn.Open(); } //create command object OleDbCommand cmd = new OleDbCommand(query, conn); int result = cmd.ExecuteNonQuery(); if (result > 0) { Response.Write("<script>alert('Sucessfully Registration!')</script>"); } else { Response.Write("<script>alert('Sorry!\n Register Failed')</script>"); } conn.Close(); } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { } }