2下拉列表和日历以在C#中显示gridview [英] 2 drop down list and calendar to display gridview in C#

查看:75
本文介绍了2下拉列表和日历以在C#中显示gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的学校项目有简单的预订票务系统,我在asp.net中使用Windows窗体。我有简单的搜索,人们可以输入bickup,drop off和date。我正在使用下拉列表FOR bickup和dropoff以及附加到文本框的日期正常日历。但我无法填充网格视图,例如在特定日期从一个位置到另一个位置的公交车,例如8月24日从A到B.我的日期我存储为日期其他Nvarchar。我可以绑定下拉列表,但搜索按钮不显示任何内容。
下面的
是我的前端代码和后端代码。我需要帮助我是c#和编码的新手。

I have simple booking ticketing system for my school project and am using windows form in asp.net. i have simple search where the person can input bickup, drop off and date. am using drop down list FOR bickup and dropoff and date normal calendar attached to text box. but i can't populate the gridview e.g bus rides available from one position to another in specific dates , like from A to B in 24 of August. my date i stored as date others Nvarchar. i can bind the drop down list but the search button shows nothing.
below is my front code and back-end code. please i need help am new to c# and coding in general.

<form id="form1" runat="server">
        <div>
            <asp:TextBox ID="tbdates" runat="server"></asp:TextBox>

            
            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" />
                 
            <asp:DropDownList ID="DropDownList1" runat="server">
            </asp:DropDownList>
   
            <asp:DropDownList ID="DropDownList2" runat="server">
            </asp:DropDownList>

        </div>
        <asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar>
        
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            CssClass="table table-hover table-striped">
            <Columns>
                <asp:BoundField DataField="BusNo" HeaderText="Bus Number" />
                <asp:BoundField DataField="date" HeaderText="Date" />
                <asp:BoundField DataField="Time" HeaderText="Time" />
                <asp:BoundField DataField="Bickup" HeaderText="Bick Up" />
                <asp:BoundField DataField="DropOff" HeaderText="Drop Off" />
                <asp:BoundField DataField="Fare" HeaderText="Fare" />
            </Columns>
            <HeaderStyle BackColor="#33CCFF" />
        </asp:GridView>
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <br />
    </form>





我尝试了什么:





What I have tried:

<pre> public partial class DriverDisplay : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                fill_DropDownList1();
                fill_DropDownList2();
            }

        }
        private void fill_DropDownList1()
        {
            try
            {
                SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase1ConnectionString"].ConnectionString);

                string sql = "SELECT * FROM Ticket";
                SqlCommand cmd = new SqlCommand(sql, con2);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                DropDownList1.DataSource = dt;
                DropDownList1.DataTextField = "Bickup";
                DropDownList1.DataValueField = "Bickup";
                DropDownList1.DataBind();
            }
            catch (Exception)
            {

            }
        }

        private void fill_DropDownList2()
        {
            try
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase1ConnectionString"].ConnectionString);

                string sql = "SELECT * FROM Ticket";
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                DropDownList2.DataSource = dt;
                DropDownList2.DataTextField = "Dropoff";
                DropDownList2.DataValueField = "DropOff";
                DropDownList2.DataBind();
            }
            catch (Exception)
            {

            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Calendar1.Visible = true;

           
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            tbdates.Text = Calendar1.SelectedDate.ToShortDateString();
            Calendar1.Visible = false;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //DateTime date = Convert.ToDateTime(tbdates.Text);
            SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase1ConnectionString"].ToString());

            sqlcon.Open();

            string query = "select * from Ticket where date = @Date";
            SqlCommand cmd = new SqlCommand(query, sqlcon);
            SqlParameter date = cmd.Parameters.Add("@Date", SqlDbType.DateTime);
           
            SqlDataReader rdr = cmd.ExecuteReader();

            GridView1.DataSource = rdr;
            GridView1.DataBind();

            sqlcon.Close();

        }
    }
}

推荐答案

You need to pass the value of the date to the parameter







protected void Button1_Click(object sender, EventArgs e)
       {
           //DateTime date = Convert.ToDateTime(tbdates.Text);
           SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase1ConnectionString"].ToString());

           sqlcon.Open();

           string query = "select * from Ticket where date = @Date";
           SqlCommand cmd = new SqlCommand(query, sqlcon);
           //here it is necessary to assign the value of the date for the query
           SqlParameter date = cmd.Parameters.Add("@Date", "date value here");

           SqlDataReader rdr = cmd.ExecuteReader();

           GridView1.DataSource = rdr;
           GridView1.DataBind();

           sqlcon.Close();

       }
   }


这篇关于2下拉列表和日历以在C#中显示gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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