[Solved] ASP.Net Error: Cannot implicitly convert type string to bool

Last Reply 4 months ago By itsme

Posted 4 months ago

I am trying to concate my drop down value but i am getting error.

Cannot implicitly convert type 'string' to 'bool'

if (ddlTimeFrom.SelectedValue) { mode.Add(ddlTimeFrom.Text);}

if (ddlTimeTo.SelectedValue) { mode.Add(ddlTimeTo.Text); }

Please suggest me

design:

<body>
    <form id="form1" runat="server">
        <div>
            <fieldset style="width: 250px">
                <legend>Select Time
                </legend>
                From: 
                <asp:DropDownList ID="ddlTimeFrom" runat="server"></asp:DropDownList>
                To:
                <asp:DropDownList ID="ddlTimeTo" runat="server"></asp:DropDownList>

            </fieldset>
        </div>
    </form>
</body>

code behind:

    public partial class dropdown : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindTime();
                List<String> mode = new List<String>();
                if (ddlTimeFrom.SelectedValue) { mode.Add(ddlTimeFrom.Text); }
                if (ddlTimeTo.SelectedValue) { mode.Add(ddlTimeTo.Text); }
                string OpenTime = string.Join(", ", mode);
            }
        }
        private void BindTime()
        {
            // Set the start time (00:00 means 12:00 AM)
            DateTime StartTime = DateTime.ParseExact("00:00", "HH:mm", null);
            // Set the end time (23:55 means 11:55 PM)
            DateTime EndTime = DateTime.ParseExact("23:55", "HH:mm", null);
            //Set 5 minutes interval
            TimeSpan Interval = new TimeSpan(0, 5, 0);
            //To set 1 hour interval
            //TimeSpan Interval = new TimeSpan(1, 0, 0);           
            ddlTimeFrom.Items.Clear();
            ddlTimeTo.Items.Clear();
            while (StartTime <= EndTime)
            {
                ddlTimeFrom.Items.Add(StartTime.ToShortTimeString());
                ddlTimeTo.Items.Add(StartTime.ToShortTimeString());
                StartTime = StartTime.Add(Interval);
            }
            ddlTimeFrom.Items.Insert(0, new ListItem("--Select--", "0"));
            ddlTimeTo.Items.Insert(0, new ListItem("--Select--", "0"));
        }
    }

 

You are viewing reply posted by: itsme 4 months ago.
Posted 4 months ago

Thanky so much .....

I also found a way to do this...

<div>
    <fieldset style="width: 250px">
        <legend>Select Time</legend>
        From:<asp:DropDownList ID="ddlTimeFrom" runat="server"></asp:DropDownList>
        To:<asp:DropDownList ID="ddlTimeTo" runat="server"></asp:DropDownList>
    </fieldset>
</div>

code behind:

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            int i = 0;
            try
            {
                List<string> mode = new List<string>();
                if (Cashpay.Checked) { mode.Add(Cashpay.Text); }
                if (Cardpay.Checked) { mode.Add(Cardpay.Text); }
                string payment = string.Join(", ", mode);
                string start = ddlTimeFrom.SelectedValue.ToString();
                string end = ddlTimeTo.SelectedValue.ToString();
                string merge = string.Join(", ", start, end);
                //Label1.Text = merge.ToString();
                // Extract Image File Name.
                string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);

                //Set the Image File Path.
                string filePath = "~/uploads/" + fileName;

                //Save the Image File in Folder.
                FileUpload1.PostedFile.SaveAs(Server.MapPath(filePath));

                string constr = ConfigurationManager.ConnectionStrings["Q_SS_regnConnectionString1"].ConnectionString;
                using (SqlConnection conn = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand("INSERT INTO Q_SP_Afterlogin (Bussiness_name,city,email,name,toll_free,website_link,payment_mode,Images,Image_name,address,h_open) VALUES (@Bname,@DropDownList2,@mail,@name,@toll,@link,@payment,@Image,@Image_name,@address,@open)", conn))
                    {
                        cmd.Parameters.AddWithValue("@Bname", DropDownList1.SelectedValue.ToString());
                        cmd.Parameters.AddWithValue("@DropDownList2", DropDownList2.SelectedValue.ToString());
                        cmd.Parameters.AddWithValue("@mail", mail.Text.Trim().ToString());
                        cmd.Parameters.AddWithValue("@toll", toll.Text.Trim().ToString());
                        cmd.Parameters.AddWithValue("@link", link.Text.Trim().ToString());
                        cmd.Parameters.AddWithValue("@name", name.Text.Trim().ToString());
                        cmd.Parameters.AddWithValue("@address", address.Text.Trim().ToString());
                        cmd.Parameters.AddWithValue("@payment", payment);
                        cmd.Parameters.AddWithValue("@Image", filePath);
                        cmd.Parameters.AddWithValue("@Image_name",fileName);
                        cmd.Parameters.AddWithValue("@open",merge);
                        conn.Open();
                        i = cmd.ExecuteNonQuery();
                        conn.Close();
                    }
                }
                if (i > 0)
                {
                    lab.Text = "Business information is added successfully.";
                    lab.Visible = true;
                }
                else
                {
                    lab.Text = "Error while adding..!!";
                }