0

hello expert, i have three dropdownlist option consist of two string and one datetime that convert into string. If i want to choose first and second dropdown without the third one which is datetime, it occur error "the string was not recognized as a valid datetime. there is an unknown word starting at index 0." but if i choose the third one itself or it combine with other dropdown list, it success. I want the user can choose the option with or without the date itself. Can you help me how to solve it ? this is my code in c#

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class loginpage_searchdata : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MESTrans"].ConnectionString);
    SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["MESCommEntities"].ConnectionString);
    SqlCommand cmd;
    SqlDataAdapter da;
    string query = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            con.Open();

            query = "select jo_no,prod_line, need_by_date from CutPanelcard";
            cmd = new SqlCommand(query, con);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                if (!string.IsNullOrEmpty(DropDownListSearchJO.SelectedValue))
                {
                    DropDownListSearchJO.Items.Add(dr[0].ToString());
                }
                if (!string.IsNullOrEmpty(DropDownListSearchLine.SelectedValue))
                {
                    DropDownListSearchLine.Items.Add(dr[1].ToString());
                }
                if (!string.IsNullOrEmpty(DropDownListSearchRec.SelectedValue))
                {
                    DropDownListSearchRec.Items.Add(dr[2].ToString());
                }
            }

            con.Close();
            gv1();
        }

    }

    private void gv1()
    {
        try
        {
            con.Open();
            query = "select * from CutPanelCard";
            cmd = new SqlCommand(query, con);
            da = new SqlDataAdapter(cmd);

            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();

            con.Close();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }

    /**  protected void DropDownListSearchJO_SelectedIndexChanged(object sender, EventArgs e)
      {
          try
          {
              con.Open();
              query = "select * from CutPanelCard where jo_no ='" + DropDownListSearchJO.SelectedItem.ToString() + "'";
              cmd = new SqlCommand(query, con);
              da = new SqlDataAdapter(cmd);

              DataSet ds = new DataSet();
              da.Fill(ds);
              GridView1.DataSource = ds;
              GridView1.DataBind();

              con.Close();
          }

          catch (Exception ex)
          {
              Response.Write(ex.Message);
          }
          GridView1.Visible = true;
      }**/

    /**protected void DropDownListSearchLine_SelectedIndexChanged(object sender, EventArgs e)
     {
         try
         {
             con.Open();
             query2 = "select * from CutPanelCard where prod_line ='" + DropDownListSearchLine.SelectedItem.ToString() + "'";
             cmd2 = new SqlCommand(query2, con);
             da2 = new SqlDataAdapter(cmd2);

             DataSet ds2 = new DataSet();
             da2.Fill(ds2);
             GridView1.DataSource = ds2;
             GridView1.DataBind();

             con.Close();
         }

         catch (Exception ex)
         {
             Response.Write(ex.Message);
         }

         GridView1.Visible = true;
     }**/

    protected void Button_Search_Click(object sender, EventArgs e)
    {
        try
        {
            con.Open();

                DataTable dt = new DataTable("UserLogin");

                    if ((!string.IsNullOrEmpty(DropDownListSearchJO.SelectedValue)) || (!string.IsNullOrEmpty(DropDownListSearchLine.SelectedValue)) || (!string.IsNullOrEmpty(DropDownListSearchRec.SelectedValue)))
                    {

                        query = "select [req_id],[prod_line] ,[jo_no],[buyer],[request_date],[need_by_date],[qty],[username] from CutPanelCard where jo_no=@jo_no OR prod_line=@prod_line OR need_by_date=@need_by_date";
                        cmd = new SqlCommand(query, con);
                        cmd.Parameters.AddWithValue("@jo_no", DropDownListSearchJO.SelectedValue);
                        cmd.Parameters.AddWithValue("@prod_line", DropDownListSearchLine.SelectedValue);
                        cmd.Parameters.AddWithValue("@need_by_date", Convert.ToDateTime(DropDownListSearchRec.SelectedValue));
                    }

                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();

                    con.Close();
        }

        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        GridView1.Visible = true;
        Button_Confirm.Visible = true;
    }

    protected void Button_Confirm_Click(object sender, EventArgs e)
    {

        DataTable dt = new DataTable("CutPanelCard");
        dt.Columns.Add(new DataColumn() { ColumnName = "req_id", DataType = typeof(String) });
        dt.Columns.Add(new DataColumn() { ColumnName = "prod_line", DataType = typeof(String) });
        dt.Columns.Add(new DataColumn() { ColumnName = "jo_no", DataType = typeof(String) });
        dt.Columns.Add(new DataColumn() { ColumnName = "buyer", DataType = typeof(String) });
        dt.Columns.Add(new DataColumn() { ColumnName = "request_date", DataType = typeof(String) });
        dt.Columns.Add(new DataColumn() { ColumnName = "need_by_date", DataType = typeof(String) });
        dt.Columns.Add(new DataColumn() { ColumnName = "qty", DataType = typeof(String) });

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
            {
                CheckBox cbSelect = GridView1.Rows[i].FindControl("cbSelect") as CheckBox;
                if (cbSelect.Checked)
                {
                    DataRow dr = dt.NewRow();
                    Label lblReq = GridView1.Rows[i].FindControl("lblReq") as Label;
                    Label lblLine = GridView1.Rows[i].FindControl("lblLine") as Label;
                    Label lblJo_no = GridView1.Rows[i].FindControl("lblJo_no") as Label;
                    Label lblBuyer = GridView1.Rows[i].FindControl("lblBuyer") as Label;
                    Label lblReqDate = GridView1.Rows[i].FindControl("lblReqDate") as Label;
                    Label lblNeed_by_date = GridView1.Rows[i].FindControl("lblNeed_by_date") as Label;
                    Label lblQty = GridView1.Rows[i].FindControl("lblQty") as Label;

                    dr["req_id"] = lblReq.Text;
                    dr["prod_line"] = lblLine.Text;
                    dr["jo_no"] = lblJo_no.Text;
                    dr["buyer"] = lblBuyer.Text;
                    dr["request_date"] = lblReqDate.Text;
                    dr["need_by_date"] = lblNeed_by_date.Text;
                    dr["qty"] = lblQty.Text;

                    dt.Rows.Add(dr);  
                }

            }

        }

        Session["EmployeeData"] = dt;

        Response.Redirect("Confirmation.aspx", true);

    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

}
4
Contributors
3
Replies
46
Views
4 Months
Discussion Span
Last Post by Raj chanda
2

Posting 200+ lines of code for a simple problem is disrespectul of the time we spend to help you. Please show the relevant code for review.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.