asp.net dropdown list default value that does not exist in sql table

asp.net dropdown list default value that does not exist in sql table

By : user2949879
Date : November 17 2020, 04:28 AM
I wish did fix the issue. You dont have to add an entry in database to show that default value in your drop down list. You can simply initialize an ListItem object and insert it to the drop down list at index 0.
Remember your drop down list is a collection of ListItem objects, so initialize an object of ListItem type and add it to the drop down list.
code :
using(SqlConnection con = new SqlConnection(cs))
    using(SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.Gender", con))
        DropDownList1.DataSource =  cmd.ExecuteReader();
        DropDownList1.DataTextField = "GenderName";
        DropDownList1.DataValueField = "ID";

        ListItem li = new ListItem("Select Gender", "-1");
        DropDownList1.Items.Insert(0, li);

Share : facebook icon twitter icon
when selecting a value from a dropdown list set other dropdown list to it's default value

when selecting a value from a dropdown list set other dropdown list to it's default value

By : Vinay Chinu
Date : March 29 2020, 07:55 AM
With these it helps I have an php form which contains 7 dropdown lists. When selecting a value from one of this I want the others 6 to return to their default values if they were previously opened. I think it should be used a javascript for this but I don't now to write such a script. I guess each of this dropdown list must have an ID and when I select an option from each one of the 7 the javascript should recognise the id and sets the other to default. I tried using a javascript with document.getElementsByName('id') but it doesn't work. , Here is a idea to start. Consider 2 dropdown like this.
code :
<select id="Numeric">
   <option value="1">1</option>
   <option value="2">2</option>
   <option value="3">3</option>

<select id="Alphabets">
   <option value="A">A</option>
   <option value="B">B</option>
   <option value="C">C</option>
var val = document.getElementById("Numeric").value;
var obj = document.getElementById("Alphabets");

if(val == "1") obj.value = "A"
else if(val == "2") obj.value = "B"
else if(val == "3") obj.value = "C"
      obj.value = "A";

      obj.value = "B";

      obj.value = "C";
    <script type="text/javascript">
     function handleSelect(thisObj){
           var list = document.getElementsByTagName("SELECT")

           for(var i=0; i< list.length; i++)
          if(list[i] ==thisObj) continue;
          list[i].value = "0";
      <form id="form1" name="form1" method="post" action="">
           <select name="select" size="1" id="1" onchange="handleSelect(this)">
               <option value="0" selected="selected">please select</option>
               <option value="1">blue</option>
               <option value="2">green</option>
               <option value="3">red</option>
    <br />
           <select name="select2" size="1" id="2" onchange="handleSelect(this)">
               <option value="0" selected="selected">please select</option>
               <option value="1">intel</option>
               <option value="2">amd</option>
    <br />
           <select name="select3" size="1" id="3" onchange="handleSelect(this)">
               <option value="0" selected="selected">please select</option>
               <option value="1">fish</option>
               <option value="2">car</option>
               <option value="3">table</option>
    <br />
            <select name="select4" size="1" id="4" onchange="handleSelect(this)">
                <option value="0" selected="selected">please select</option>
                <option value="1">lcd</option>
                <option value="2">led</option>
Using Linq to determine if a record does not exist in a list but does exist in a table

Using Linq to determine if a record does not exist in a list but does exist in a table

By : Son Zion
Date : March 29 2020, 07:55 AM
this will help Looks like you've reversed the logic there. If you want to check for the existence of a particular record, the ID of which is stored in list.EntityID, it looks like this:
code :
bool exists = db.table.Any(t => t.EntityID == list.EntityID);
var extraitems = db.table.Where(t => !list.Select(l => l.EntityID).Contains(t.EntityID));
asp.net - Setting the default value for a dropdown list based on the value of another dropdown list

asp.net - Setting the default value for a dropdown list based on the value of another dropdown list

By : user2304251
Date : March 29 2020, 07:55 AM
I wish this helpful for you You have to set OnSelectedIndexChanged to your gender dropdown and for a start you will also set the AutoPostBack property to true, like so:
code :
<asp:DropDownList id="ddlGender" runat="server" OnSelectedIndexChanged="ddlGender_OnSelectedIndexChanged" AutoPostBack="true">
void ddlGender_OnSelectedIndexChanged(Object sender, EventArgs e)
    if (ddlGender.SelectedValue == "F")
        ddlSelective.SelectedValue = "N/A"
    // more code here
Role ID does not exist when i using dynamic dropdown list

Role ID does not exist when i using dynamic dropdown list

By : Jack Yin
Date : March 29 2020, 07:55 AM
like below fixes the issue AFAIK, AddToRoleAsync method requires 2 input parameters: user ID and role name as string, not role GUID.
The main problem is SelectList on first Register action method passed RoleID content for second input parameter instead of RoleName, which throwing exception since role GUID certainly doesn't exist for role name when processing POST request. Hence, controller code should be changed to pass role name as value on DropDownListFor:
code :
public ActionResult Register()
    var roles = db.Roles.Select(r => new { RoleID = r.Id, RoleName = r.Name }).ToList();
    ViewBag.Roles = new SelectList(roles, "RoleName", "RoleName");

    return View();
Set a default value on dropdown list <select> from the value in a table in PHP

Set a default value on dropdown list <select> from the value in a table in PHP

By : user1487720
Date : March 29 2020, 07:55 AM
seems to work fine I found a very easy solution for the problem. By adding the option of data-select in the anchor tag and adding a javascript for on click function. Below is the code changes.
code :
    <td align="center">
    <a href="#exp" data-select="1">1</a><br>
  <a href="#exp" data-select="1"">2</a><br>
  <a href="#exp" data-select="1"">3</a><br>
<select class="form-control  input-lg" id="experience"name="experience" 
  placeholder="Experience (in Years)" required="">
 <option value="">Experience: </option>
              $sql="SELECT * FROM experience";
              if($result->num_rows > 0) {
                while($row = $result->fetch_assoc()) {
                  echo "<option value='".$row['value']."' data- 
        <script type="text/javascript">

    var $expe = $('#experience');
$('a[href="#exp"]').click(function () {
    $expe.val( $(this).data('select') );
Related Posts Related Posts :
  • Marshal.Copy attempted to read or write protected memory At Random Times
  • Restrict Type variable to specific class or subclass
  • Horizontal text alignment in a PdfPCell
  • C# crashing with Form.show() command, ObjectDisposedException - Deeper look / explanation please
  • Will the result of a LINQ query always be guaranteed to be in the correct order?
  • "Could not find default endpoint element that references contract"
  • Umbraco Request.QueryString is null if it's the first time the page is loaded
  • Error inconsistent accessibility method C#
  • How to program Intel Xeon Phi with C#?
  • remove nested element using regular expression
  • Is there a C# alternative to Java's vararg parameters?
  • Clear particular column values in DataTable
  • how to add event handler to programatically created checkboxes
  • Cannot apply indexing with [] to an expression of type 'System.Collections.Specialized.NameValueCollection'
  • Check for key in pre-existing dictionary in case insensitive manner
  • How to remove year from datetime object?
  • Accessing Settings in different ways
  • "This project is empty" error in Sonarqube
  • How to create reusable icon menu in Xamarin
  • Value Cannot be null in Ado.Net connectivity
  • Adding a custom/dynamic attribute when using XSD.exe
  • How to convert object to correct type
  • Automatically sign out from Forms Authentication in ASP.NET when browser is closed
  • Can a WCF service support both Buffered and Streamed transfer modes?
  • Verify a CA Certificate with a public key in C#
  • How to invoke a Web Service that requires the "patch" verb using the C# WebClient wrapper?
  • Proper way a implementing property based on generic type
  • Closing a form that is created in another thread
  • How Can You Bind a List<String> to a StackPanel
  • WPF Application Update Best Practices - Architectural Explanation
  • System.UnauthorizedAccessException in Server.MapPath()
  • Connecting and Using SQL Compact Edition in a WPF application
  • C#: weird ref in constructor to behave like "virtual field"
  • C# XDocument Load with multiple roots
  • How to decide what goes in the Domain or Application Project in a "DDD" solution?
  • How to get/set a property of an interface that is not always implemented
  • Read-only array field in unsafe struct
  • i got "Invalid attempt to call Read when reader is closed" when using sqldatareader how to solve it in a three
  • Why should I encapsulate objects in using if there is garbage collection
  • How to load Word document from byte array
  • Caliburn.Micro and ContextMenu for DataGrid Row
  • Linq "join" with a IList<T> getting "Error Unable to create a constant value.."
  • How to draw red wavy line under words in RichTextBox c# winform
  • HttpPostedFileBase returns Null MVC3
  • Refresh Dropdownlist in webform
  • How to convert serialized byte array back to its text form
  • How to do a loop to check all the variables at the same time for C#?
  • Facebook Sentiment Analysis API
  • Counting occurrences of specific letters in string
  • Module 'System.Data.Linq' Version=, Culture=neutral, Publickeytoken=b77a5c561934e089' should be referenced
  • C# MVC Dynamically create view model
  • Get Executing Path of C# application when using nunit with Jenkins
  • GDI count stable near 100 but Handle count keep increasing
  • Problem adding string value to ViewBag from an MVC Action using TempData
  • Reading Excel file on condition
  • what is windows programming from C++, C# and Java perspective?
  • How to deal with C# object references in MongoDB?
  • How to create a list from filtering 2 lists with linq to object
  • Webclient calls to DownloadString cause my app to freeze
  • How to create a JSON WebService in c# ASP.Net with a valid JSON output and query with JQuery/Ajax
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org