0

Hi again,

I need an professional advise how to implement dropdownlist in very professional way. So, my code is:

<asp:DropDownList id="ddlStudents" runat="server" CssClass="textbox" OnSelectedIndexChanged="ddlStudents_SelectedIndexChanged" AutoPostBack="true" EnableViewState="true">
      <asp:ListItem Value="0">Choose</asp:ListItem>
    </asp:DropDownList>

The rest of data I get from SQL DB. Is it the right way to populate the default value by:

<asp:ListItem Value="0">Choose</asp:ListItem>

?

Thanks for future advice.

P.S Also, I have a label which shows a record from DB. DateTime. I need to format it someway that instead of "Birth: 15/10/2000 00:00:00" it will show only "Birth: 15/10/2000". I use SQL2005 for this project and there's no 'date' type.

Edited by Nfurman: n/a

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by Nfurman
0

when you are binding the dropdown from db then
after binding the dropdown you should right

ddlName.Items.Insert(0, new ListItem(Choose, "0"))

and dont insert listitem on HTML

0

I have a label which shows a record from DB. DateTime. I need to format it someway that instead of "Birth: 15/10/2000 00:00:00" it will show only "Birth: 15/10/2000".

You can do it in many ways,

In Code:
String.Format("{0:dd/MM/yyyy}", yourDateTime);

Or

In SQL:
SELECT CONVERT(varchar(10),yourDateTimeTyeColumnName,103)

0

when you are binding the dropdown from db then
after binding the dropdown you should right

ddlName.Items.Insert(0, new ListItem(Choose, "0"))

and dont insert listitem on HTML

Hi Malashukla, thanks for your advise, this method works. I say, it puts the 0 value for "Choose" text. You must have made a little mistake in your example by not inserting Choose word between the quotation marks. Please see how I've used it:

protected void GetStudents()
    {
        SqlConnection conn = new SqlConnection(connection);
        SqlCommand cmd = new SqlCommand("GetFullStudentProfile", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataReader reader;
        {
            try
            {
                conn.Open();
                reader = cmd.ExecuteReader();
                //ddlStudents.Items.Insert(0, new ListItem("Choose", "0"));
                while (reader.Read())
                {
                    
                    ListItem item = new ListItem();
                    item.Text = reader["StudentName"].ToString() + " " + reader["StudentFamilyname"].ToString();
                    item.Value = reader["StudentID"].ToString();
                    ddlStudents.Items.Insert(0, new ListItem("Choose", "0"));
                    ddlStudents.Items.Add(item);
                    
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                lbl.Text = ex.Message;
            }
            finally
            {
                conn.Close();
            }
        }
    }

I have much more studs in my DB.

Edited by Nfurman: n/a

Attachments print.png 69.35 KB
0

You can do it in many ways,

In Code:
String.Format("{0:dd/MM/yyyy}", yourDateTime);

Or

In SQL:
SELECT CONVERT(varchar(10),yourDateTimeTyeColumnName,103)

Thank you Knvn very much.

I used the code-side approach and it worked perfect.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.