can I set a drop down list item to have a value = Null

Last post 05-12-2008 11:36 AM by cluce. 19 replies.

Sort Posts:

  • can I set a drop down list item to have a value = Null

    05-09-2008, 12:24 PM
    • Member
      89 point Member
    • cluce
    • Member since 01-25-2008, 5:37 PM
    • Posts 410

    I have a table in a database that populates a drop down list and I also have a list item that is appended with the value = 0.  Is there any way to set this value to be Null because having the value = 0 is giving me a FKey constraint error.  The idea of my web page is:  I have 6 drop down lists each with its own list of parts.  I want to the user to be able to mix and match parts according to selection and click the button event to insert the parameter values into the database.  But the trick is to allow the drop down list to insert Null values because the user may only want to match 2 parts from 2 drop down lists and the other 4 drop down list need to insert Null values. can someone give me any ideas on how to fix this?

    here is the code for one dropdown list. I figured if I can get one to insert a null value,  I can get all of them to work. 

    <asp:DropDownList ID="ddlEngine" runat="server"
    DataSourceID="ObjectDataSourceEngine" DataTextField="description"
    DataValueField="engineID" Height="20px" Width="200px"
    AppendDataBoundItems="true" >
    <asp:ListItem Value="0" Selected="True">none</asp:ListItem>
    </asp:DropDownList>

     

  • Re: can I set a drop down list item to have a value = Null

    05-09-2008, 12:54 PM
    • Contributor
      4,089 point Contributor
    • TonyDong
    • Member since 02-01-2006, 6:30 PM
    • BC, Canada
    • Posts 802

     

    Why not wrote code to check if select value == 0; conver it to null so you don't need to change any dropdownlist.
    Don't forget to click "Mark as Answer" on the post(s) that helped you.
  • Re: can I set a drop down list item to have a value = Null

    05-09-2008, 1:10 PM
    • Member
      89 point Member
    • cluce
    • Member since 01-25-2008, 5:37 PM
    • Posts 410

    can you give me an example how to convert it?

  • Re: can I set a drop down list item to have a value = Null

    05-09-2008, 1:24 PM
    • Contributor
      3,394 point Contributor
    • lberan
    • Member since 02-12-2008, 7:23 PM
    • Pittsburgh, PA
    • Posts 804

    SELECT NULL AS yourColumnName UNION SELECT yourComunName from yourTable

    this insert a null value to your list

    Lynn

    Please mark replies that have helped you as answers.
  • Re: can I set a drop down list item to have a value = Null

    05-09-2008, 1:28 PM
    • Contributor
      4,089 point Contributor
    • TonyDong
    • Member since 02-01-2006, 6:30 PM
    • BC, Canada
    • Posts 802
    public static DateTime? ToDateTime(string s)

    {

    if (string.IsNullOrEmpty(s)) return null;

    if (s.Trim() == string.Empty) return null;

    try

    {

    return (DateTime?)Convert.ToDateTime(s);

    }

    catch (FormatException ex)

    {

    throw new FormatException(string.Format("Parameter {0} is not a correct format !", s), ex);

    }

    }

    /// <summary>

    /// Convert empty or null to default value

    /// </summary>

    /// <param name="s">Input value</param>

    /// <param name="defaultValue">default value</param>

    /// <returns>return default value or input value</returns>

    public static DateTime? ToDateTime(string s, DateTime defaultValue)

    {

    if (string.IsNullOrEmpty(s)) return defaultValue;

    if (s.Trim() == string.Empty) return defaultValue;

    return ToDateTime(s);

    }

    Don't forget to click "Mark as Answer" on the post(s) that helped you.
  • Re: can I set a drop down list item to have a value = Null

    05-09-2008, 1:29 PM
    • Contributor
      4,089 point Contributor
    • TonyDong
    • Member since 02-01-2006, 6:30 PM
    • BC, Canada
    • Posts 802

    /// <summary>

    /// Convert empty or null to null

    /// </summary>

    /// <param name="s">Input value</param>

    /// <returns>return null or input value</returns>

    public static DateTime? ToDateTime(string s)

    {

    if (string.IsNullOrEmpty(s)) return null;

    if (s.Trim() == string.Empty) return null;

    try

    {

    return (DateTime?)Convert.ToDateTime(s);

    }

    catch (FormatException ex)

    {

    throw new FormatException(string.Format("Parameter {0} is not a correct format !", s), ex);

    }

    }

    /// <summary>

    /// Convert empty or null to default value

    /// </summary>

    /// <param name="s">Input value</param>

    /// <param name="defaultValue">default value</param>

    /// <returns>return default value or input value</returns>

    public static DateTime? ToDateTime(string s, DateTime defaultValue)

    {

    if (string.IsNullOrEmpty(s)) return defaultValue;

    if (s.Trim() == string.Empty) return defaultValue;

    return ToDateTime(s);

    }

    Don't forget to click "Mark as Answer" on the post(s) that helped you.
  • Re: can I set a drop down list item to have a value = Null

    05-09-2008, 1:33 PM
    • Contributor
      3,394 point Contributor
    • lberan
    • Member since 02-12-2008, 7:23 PM
    • Pittsburgh, PA
    • Posts 804

    i really hope you wouldn't try the other method since it would be so easy just to insert a null value to the list

    Lynn

    Please mark replies that have helped you as answers.
  • Re: can I set a drop down list item to have a value = Null

    05-09-2008, 1:36 PM
    • Member
      89 point Member
    • cluce
    • Member since 01-25-2008, 5:37 PM
    • Posts 410

    Fistrt off, I am using an object data source to inert my vales. SO I need to figure out a way using this method.  

    'inserts new match

    Dim componentTypeMatchingAdapter As New managementTableAdapters.componentTypeMatchingTableAdapter

    componentTypeMatchingAdapter.InsertCompTypeMatch(ddlMount.SelectedValue, ddlInstruments.SelectedValue, ddlEngine.SelectedValue, ddlPaint.SelectedValue, ddlGenerator.SelectedValue, ddlCool.SelectedValue, ddlElectrical.SelectedValue, ddlSafety.SelectedValue)

    GridView1.DataBind()

    lblStatus.Text = "New record was added to the database."

  • Re: can I set a drop down list item to have a value = Null

    05-09-2008, 1:41 PM
    Answer
    • Contributor
      4,089 point Contributor
    • TonyDong
    • Member since 02-01-2006, 6:30 PM
    • BC, Canada
    • Posts 802

    Try the follow way, it is easy for you. 

    Dim componentTypeMatchingAdapter As New managementTableAdapters.componentTypeMatchingTableAdapter componentTypeMatchingAdapter.InsertCompTypeMatch(

    ddlMount.SelectedValue==0?null:ddlMount.SelectedValue,

    ddlInstruments.SelectedValue, ddlEngine.SelectedValue, ddlPaint.SelectedValue, ddlGenerator.SelectedValue, ddlCool.SelectedValue, ddlElectrical.SelectedValue, ddlSafety.SelectedValue) GridView1.DataBind() lblStatus.Text = "New record was added to the database."

    Don't forget to click "Mark as Answer" on the post(s) that helped you.
  • Re: can I set a drop down list item to have a value = Null

    05-09-2008, 1:47 PM
    • Contributor
      3,394 point Contributor
    • lberan
    • Member since 02-12-2008, 7:23 PM
    • Pittsburgh, PA
    • Posts 804

    the above solution is much better

    Lynn

    Please mark replies that have helped you as answers.
  • Re: can I set a drop down list item to have a value = Null

    05-09-2008, 2:00 PM
    • Member
      89 point Member
    • cluce
    • Member since 01-25-2008, 5:37 PM
    • Posts 410

    thanks for the replies. now I am getting a specified cast is not valid error??

     

     

       Protected Sub btnAddComp_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddComp.Click
    
            'inserts new match
            Dim componentTypeMatchingAdapter As New managementTableAdapters.componentTypeMatchingTableAdapter
            componentTypeMatchingAdapter.InsertCompTypeMatch(IIf(ddlMount.SelectedValue = 0, Nothing, ddlMount.SelectedValue), ddlInstruments.SelectedValue, IIf(ddlEngine.SelectedValue = 0, Nothing, ddlEngine.SelectedValue), ddlPaint.SelectedValue, ddlGenerator.SelectedValue, ddlCool.SelectedValue, ddlElectrical.SelectedValue, ddlSafety.SelectedValue)
            GridView1.DataBind()
            lblStatus.Text = "New record was added to the database."
    
            'goes to add a match view
            MultiView1.ActiveViewIndex = "1"
        End Sub
     

     

    p.s.- my code is vb.net
  • Re: can I set a drop down list item to have a value = Null

    05-09-2008, 3:17 PM
    • Contributor
      4,089 point Contributor
    • TonyDong
    • Member since 02-01-2006, 6:30 PM
    • BC, Canada
    • Posts 802

     

    Check is that InsertCompTypeMatch support parameter Nothing?
    Don't forget to click "Mark as Answer" on the post(s) that helped you.
  • Re: can I set a drop down list item to have a value = Null

    05-09-2008, 3:21 PM
    • Member
      89 point Member
    • cluce
    • Member since 01-25-2008, 5:37 PM
    • Posts 410

    I am using SQL server 2005 and the allow Nulls property is set to 'yes' on all my columns. Is there something else I need to check?

  • Re: can I set a drop down list item to have a value = Null

    05-09-2008, 3:27 PM
    • Contributor
      4,089 point Contributor
    • TonyDong
    • Member since 02-01-2006, 6:30 PM
    • BC, Canada
    • Posts 802

     

    I mean for the parameter in function

     In C# need to use

    private void Method (int? id)

    {

    }

    is your vb.net has the same sentence?

    Don't forget to click "Mark as Answer" on the post(s) that helped you.
  • Re: can I set a drop down list item to have a value = Null

    05-09-2008, 3:39 PM
    • Member
      89 point Member
    • cluce
    • Member since 01-25-2008, 5:37 PM
    • Posts 410

    I am not sure if the parameter excepts System.Nullable class.  I am using a dataset with several Table adapters. This one is unsing the componentTypeTableadapter with an Insert stored procedure as one of the methods. All I do is call this procedure like the following and fill in the parameters.

     

        Protected Sub btnAddComp_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddComp.Click
    
            'inserts new match
            Dim componentTypeMatchingAdapter As New managementTableAdapters.componentTypeMatchingTableAdapter
            componentTypeMatchingAdapter.InsertCompTypeMatch(ddlMount.SelectedValue, ddlInstruments.SelectedValue, ddlEngine.SelectedValue, 
            ddlPaint.SelectedValue, ddlGenerator.SelectedValue, 
            ddlCool.SelectedValue, ddlElectrical.SelectedValue,
            ddlSafety.SelectedValue)
    
            GridView1.DataBind()
            lblStatus.Text = "A new record was added to the database."
    
            'goes to add a match view
            MultiView1.ActiveViewIndex = "1"
        End Sub
     Maybe I might have to hardcode most of this for this to work??
Page 1 of 2 (20 items) 1 2 Next >