简体   繁体   中英

ASP.NET DropDown and SQL C#

I have a number in a database (eg 12) and I want to display that number in the dropdown list on a webform, however, not the number itself but a range of numbers from 1 to 12 (so 1,2,3....12). Is there a property I can use or a way to get a list of numbers from SQL Statement?

  1. Read the Number from Database

  2. Display a full range of numbers from 1 to X (X = Number from Database)

You just read the number from the database and then use a for loop:

for(int i = readNumber; i > 0; i--)
{
    //Add i to your dropDown list or do anything you want with it
}

将下拉列表绑定到:

Enumerable.Range(1, <number from database>);

First create an array or list and then iterate till the number you read from database then add these numbers to your array or list then bind with your dropdown datasource

var numbers = new List<int>();
for(var i = DB_NUMBER; i >= 1; i--)
{
    numbers.Add(i);
}
yourDropDown.DataSource = numbers;
yourDropDown.DataBind();

Hope it helps

Since, you wish to return the range of numbers from the SQL Query you might need a complex query, but it will always have some limit to it.

SQL Query as per your requirement:

Select Value from
(
SELECT ones.n + 10*tens.n + 100*hundreds.n + 1000*thousands.n as Value
FROM (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) ones(n),
     (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) tens(n),
     (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) hundreds(n),
     (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) thousands(n)
) as tbl
Where value between 0 and 12
ORDER BY 1

I Modified above SQL from the Original Source : Answer

C#:

 DropDownList.DataSource = DataTable;
 DropDownList.DisplayField = Value;
 DropDownList.ValueField = Value;
 DropDownList.DataBind();

To achieve this you need to

  • Take the desire number from database

  • then you have to write a for loop.

     private void InitializeDropDownList(int number) { for (int i = 0; i < number; i++) { ddlNumberRange.Items.Add(new ListItem { Text = (i + 1).ToString(), Value = (i + 1).ToString() }); } }

Based on the question

If I assume that you want SQL to generate and return the range then you want to use a recursive cte to build it from the value in your table ...

// construct the db connection and command object
var con = new SqlConnection("Your Connection String");
using(var cmd = new SqlCommand(con) { CommandType = CommandType.Text })
{
    // tell the command what SQL query we want to execute
    cmd.CommandText = @"

DECLARE @startnum INT=1
DECLARE @endnum INT= SELECT TOP 1 Number FROM ValueTable
;
WITH gen AS (
    SELECT @startnum AS num
    UNION ALL
    SELECT num+1 FROM gen WHERE num+1<=@endnum
)
SELECT * FROM gen
option (maxrecursion 100)

";

    // connect to the db and execute the command
    con.Open();
    using(var reader = cmd.ExecuteReader())
    {
        // build the range from the values generated by it
        var range = new List<int>();
        while(reader.Read()) { range.Add(reader.Read()); }

        // bind the results to the drop down on the page   
        DropDownList.DataSource = range
          .Select(i = > new { Key = i, Value = i })
          .ToArray();
        DropDownList.DisplayField = "Key";
        DropDownList.ValueField = "Value";
        DropDownList.DataBind();
    }
    con.Close();
}

The simplest approach

ok querying a db is a pretty well documented problem so I won't repeat that here. But lets assume you have the following ...

// sourced from your db
int start = 1;
int end = 12;

... from there you can build a range of values ...

var range = Enumerable.Range(start, end)
    .Select(i = > new { Key = i, Value = i })
    .ToArray();

... and then bind that range to your drop down on the page ...

DropDownList.DataSource = range;
DropDownList.DisplayField = "Key";
DropDownList.ValueField = "Value";
DropDownList.DataBind();

Sources of information ...

How to generate a range of numbers between two numbers?

https://msdn.microsoft.com/en-us/library/fksx3b4f.aspx

https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader(v=vs.110).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM