为什么我的下拉列表没有工作? [英] Why my drop down list did not working?

查看:70
本文介绍了为什么我的下拉列表没有工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用OleDB阅读器从我的下拉列表中读取excel表中的数据,但它没有工作。我尝试将插入和选择的查询结合起来,但它只在INSERT上起作用,但对SELECT不起作用。对我的问题有什么解决方案吗?



我尝试过:



I try to read data from excel sheet for my drop down list using OleDB reader but it did not working. I try to combine the query for insert and select but it just function on INSERT only but for SELECT not. It's there any solution for my problem?

What I have tried:

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

}
}

推荐答案

([UserNTID],[SerialNumber],[Model],[Department])VALUES('+ TextBox1.Text +','+ TextBox2。 Text +','+ TextBox3.Text +','+ TextBox4.Text +');
query + =SELECT * FROM [Sheet1
([UserNTID], [SerialNumber], [Model], [Department]) VALUES('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "')"; query += "SELECT * FROM [Sheet1


([Country])VALUES('+ DropDownList1 +');
//提供连接
OleDbConnection conn = new OleDbConnection(ConStr);
//检查连接状态是关闭或不关闭
//打开连接
if(conn.State == ConnectionState.Closed)
{
conn.Open();
}
//创建命令对象
OleDbCommand cmd = new OleDbCommand(query,conn);
int result = cmd.ExecuteNonQuery();
if(结果> 0)
{
Response.Write(< script> alert('Sucessfully Registration!')< / script>);
}
其他
{
Response.Write(< script> alert('抱歉!\ n注册失败')< / script>);
}
conn.Close();
}

protected void DropDownList1_SelectedIndexChanged(object sender,EventArgs e)
{

}
}
([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) { } }


虽然我们的问题并不完全清楚,但乍看之下
,你的 SELECT 查询是错误的。

Although your question is not completely clear to us,
at first glance, your SELECT query is wrong.
SELECT * FROM [Sheet1


这篇关于为什么我的下拉列表没有工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆