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

Last Reply 24 days ago By itsme

Posted 24 days 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"));
        }
    }

 

Posted 24 days ago

Hi itsme,

Check this example. Now please take its reference and correct your code.

HTML

<fieldset style="width: 250px">
    <legend>Select Time </legend>
    From: <asp:DropDownList ID="ddlTimeFrom" runat="server" OnSelectedIndexChanged="SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
    To: <asp:DropDownList ID="ddlTimeTo" runat="server" OnSelectedIndexChanged="SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
</fieldset><br />
<asp:Label ID="lblOpenTime" runat="server" />

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        BindTime();
    }
}

protected void SelectedIndexChanged(object sender, EventArgs e)
{
    List<String> mode = new List<String>();
    if (ddlTimeFrom.SelectedIndex > 0 && ddlTimeTo.SelectedIndex > 0)
    {
        mode.Add(ddlTimeFrom.Text);
        mode.Add(ddlTimeTo.Text);
        lblOpenTime.Text = "Open Time : " + string.Join(", ", mode);
    }
    else
    {
        lblOpenTime.Text = "";
    }
}

private void BindTime()
{
    DateTime StartTime = DateTime.ParseExact("00:00", "HH:mm", null);
    DateTime EndTime = DateTime.ParseExact("23:55", "HH:mm", null);
    TimeSpan Interval = new TimeSpan(0, 5, 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"));
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        BindTime()
    End If
End Sub

Protected Sub SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim mode As List(Of String) = New List(Of String)()
    If ddlTimeFrom.SelectedIndex > 0 AndAlso ddlTimeTo.SelectedIndex > 0 Then
        mode.Add(ddlTimeFrom.Text)
        mode.Add(ddlTimeTo.Text)
        lblOpenTime.Text = "Open Time : " & String.Join(", ", mode)
    Else
        lblOpenTime.Text = ""
    End If
End Sub

Private Sub BindTime()
    Dim StartTime As DateTime = DateTime.ParseExact("00:00", "HH:mm", Nothing)
    Dim EndTime As DateTime = DateTime.ParseExact("23:55", "HH:mm", Nothing)
    Dim Interval As TimeSpan = New TimeSpan(0, 5, 0)
    ddlTimeFrom.Items.Clear()
    ddlTimeTo.Items.Clear()
    While StartTime <= EndTime
        ddlTimeFrom.Items.Add(StartTime.ToShortTimeString())
        ddlTimeTo.Items.Add(StartTime.ToShortTimeString())
        StartTime = StartTime.Add(Interval)
    End While
    ddlTimeFrom.Items.Insert(0, New ListItem("--Select--", "0"))
    ddlTimeTo.Items.Insert(0, New ListItem("--Select--", "0"))
End Sub

Screenshot


Posted 24 days 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..!!";
                }