Click here to Skip to main content
15,918,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();

        }
    }
}
Posted
Updated 23-Aug-18 5:08am
v3

1 solution

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();

       }
   }
 
Share this answer
 
Comments
Nurto 23-Aug-18 11:20am    
Date value would be what i really don't know am so sorry. please can you tell me.or explain to me.
M4rLask 23-Aug-18 12:32pm    
You must take the value that the user selects in the calendar and assign the parameter
Nurto 23-Aug-18 14:40pm    
am passing text box which is tbdates.Textbox sir is that what i should put. am so sorry if it is silly question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900