Use C# to Populate Dropdown Lists With XML

Click here to see it work

The code below opens an xml file, reads the data from the xml file, dynamically adds a label for the dropdown list, and adds the dropdown list to the asp.net page. After the dropdown lists are added to the screen, the dropdown lists are populated. The dropdown list is added to an asp Panel control named pnlMain.

C# Code (You will need to reference System.Xml and System.IO in order for this to work.)

        private void LoadData(string sFile)
        {            
            XmlTextReader reader = new XmlTextReader(sFile); //the file name is passed into the function



            string sField = "";
            string sID = "";
            string sValue = "";
            string sText = "";
            string sLabel = "";


            while (reader.Read())
            {

                switch (reader.NodeType)
                {

                    case XmlNodeType.Element:
                        sField = reader.Name;
                        break;

                    case XmlNodeType.Text:

                        switch (sField)
                        {
                            case "ID": //get the ID for the dropdown from the xml file
                                sID = reader.Value;
                                break;
                            case "label":
                                sLabel = reader.Value;  //get the label name from the xml file
                                Label lblHold = new Label();
                                lblHold.ID = "lbl" + sLabel;
                                lblHold.Text = sLabel;
                                pnlMain.Controls.Add(lblHold);  //add the label to the panel control
                                DropDownList ddListHold = new DropDownList(); //declare the dropdown variable
                                ddListHold.ID = sID;
                                ddListHold.CssClass = "btn dropdown-toggle"; //add bootstrap formatting to the dropdown                                
                                ddListHold.BackColor = System.Drawing.ColorTranslator.FromHtml("#0E3FE7"); //optional color formatting
                                ddListHold.ForeColor = System.Drawing.Color.White; //optional color formatting
                                ddListHold.Width = 300; //set width of the dropdown                                                                
                                pnlMain.Controls.Add(new LiteralControl("< p>")); //add paragraph beginning tag                                
                                pnlMain.Controls.Add(ddListHold); //add the dropdown to the panel control                                
                                pnlMain.Controls.Add(new LiteralControl("< /p>")); //add paragraph ending tag
                                break;
                            case "value":
                                string[] s = reader.Value.Split('|'); //use the split function to retrieve the value and text for the dropdown items
                                sValue = s[0].ToString();
                                sText = s[1].ToString();
                                DropDownList ddListHold2 = (DropDownList)pnlMain.FindControl(sID); //find the previously added dropdown
                                ddListHold2.Items.Add(new ListItem(sText, sValue)); //add the list item to the previously added dropdown
                                break;
                        }
                        break;
                    case XmlNodeType.EndElement: //Display the end of the element.
                        break;
                }
            }
        }

XML File Contents

      < ddlists>
        < dddata>
          < ID>ddRegion< /ID>
          < label>Region< /label>
          < value>0|Select a Region< /value>
          < value>1|East< /value>
          < value>2|North< /value>
          < value>3|South< /value>
          < value>4|West< /value>
        < /dddata>
        < dddata>
          < ID>ddRepresentative< /ID>
          < label>Representative< /label>
          < value>0|Select a Representative< /value>
          < value>1|Jordan, Larry< /value>
          < value>2|McNabb, Deion< /value>
          < value>3|Rodgers,Brett< /value>
          < value>4|Smith, John< /value>
        < /dddata>
      < /ddlists>    

#CSharp #Bootstrap #XML

Posted in Visual Studio Code Examples

Add a C# Dropdown to a Bootstrap Modal Dialog

Add a C# Dropdown to a Bootstrap Modal Dialog

Click here to see it work

This example combines C# and Bootstrap to create a bootstrap modal dialog containing an asp dropdown and an asp button to retrieve selected values from the dropdown and transfer those values to the main form.

First, I added code to the page load event to hide the dialog when the form is loaded. I added the runat=server tag and the ID tag to the modal dialog so that it can be referenced from the code behind in c#. By adding an asp label to the title section of the modal dialog, I can change the title from code. I also added an asp label for the dropdown. I added the btn and dropdown-toggle classes to the asp dropdown to give the appearance of a bootstrap dropdown.

The Save button contains code to retrieve the selected value and selected text from the drop down, and code to hide the modal dialog.

        < div class="col-lg-2">< /div>
            < div class="col-lg-8">
            < asp:Label ID="lblSelectedValue" runat="server" Text="Selected Value">< /asp:Label>< br />
            < asp:TextBox ID="txtSelectedValue" runat="server" ReadOnly="True">< /asp:TextBox>< br />
            < asp:Label ID="lblSelectedText" runat="server" Text="Selected Text">< /asp:Label>< br />
            < asp:TextBox ID="txtSelectedText" runat="server" ReadOnly="True">< /asp:TextBox>< br />< br />
            < asp:Button class="btn" ID="btnOpen" runat="server" Text="Open Dialog" style="background-color:#0E3FE7;color:white" OnClick="btnOpen_Click" />
            < /div>
            < div class="col-lg-2">< /div>
            < div class="col-lg-12" style="z-index:9999" id="modalExample" runat="server">
                < div class="modal-dialog">
                    < div class="fade in modal-content">
                        < div class ="modal-header">
                            < asp:Label ID="lblTitle" runat="server" Text="">< /asp:Label>
                        < /div>
                        < div class="modal-body">
                            < asp:Label ID="lblValue" runat="server" Text="">< /asp:Label>
                            < asp:DropDownList style="background-color:#0E3FE7;color:white" class="btn dropdown-toggle" ID="ddValueList" runat="server" >
                                < asp:ListItem Value="0">N/A< /asp:ListItem>
                                < asp:ListItem Value="1">Value 1< /asp:ListItem>
                                < asp:ListItem Value="2">Value 2< /asp:ListItem>
                                < asp:ListItem Value="3">Value 3< /asp:ListItem>
                            < /asp:DropDownList>
                        < /div>
                        < div class="modal-footer">
                            < asp:Button class="btn" style="background-color:#0E3FE7;color:white" ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
                        < /div>                    
                    < /div>
                < /div>        
            < /div>

C# Code

        protected void Page_Load(object sender, EventArgs e)
        {
            modalExample.Visible = false;
            lblTitle.Text = "Value Selection";
            lblValue.Text = "You must select a value from the list below.";
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            txtSelectedValue.Text = ddValueList.SelectedItem.Value;
            txtSelectedText.Text = ddValueList.SelectedItem.Text;
            modalExample.Visible = false;
        }
        protected void btnOpen_Click(object sender, EventArgs e)
        {
            modalExample.Visible = true;
        }
      

This example was created using the Community Version of Visual Studio 2019.

#VisualStudio #CSharp #Bootstrap

Posted in Visual Studio Code Examples

SQL Server – Add Column Script

–Add a column to an existing table

ALTER TABLE dbo.Employee
ADD Active varchar(1)
GO

–Add default to new column

ALTER TABLE dbo.Employee
ADD CONSTRAINT Employee_Active 
DEFAULT (‘N’) FOR Active
GO

Tagged with: , , , ,
Posted in Visual Studio Code Examples

SQL Server List Tables And Columns

–List Column Names

SELECT COLUMN_NAME, TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG = ‘YOUR_DB_NAME’

–List Tables

SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_CATALOG = ‘YOUR_DB_NAME’
–List Views

SELECT *
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_CATALOG = ‘YOUR_DB_NAME’

–List All Columns in All Views

SELECT *
FROM INFORMATION_SCHEMA.VIEW_COLUMN_USAGE
WHERE TABLE_CATALOG = ‘YOUR_DB_NAME’
–List Stored Procedures

SELECT *
FROM sys.procedures

–List a Stored Procedure Script

EXEC sp_HelpText N’YourStoredProc’

Tagged with: , , , , ,
Posted in SQL Server

SQL Server Add Default To Existing Table

–String Value
ALTER TABLE [Table1] ADD CONSTRAINT [Table1_Column1] DEFAULT (‘Not Applicable’) FOR [Column1]
GO

–Numeric Value
ALTER TABLE [Table1] ADD CONSTRAINT [Table1_Column1] DEFAULT ((0)) FOR [Column1]
GO

–Date Value
ALTER TABLE [Table1] ADD CONSTRAINT [Table1_Column1] DEFAULT (((1)/(1))/(1900)) FOR [Column1]
GO

Tagged with: , , , , , ,
Posted in SQL Server

HTML Table Example

The example below displays how to create a table using HTML and the <table> tag.

The example below consists of the following elements:
<table> This tag begins the table.

border = “1” This attribute defines the width of the border. If this attribute is not added to the table tag, no border will be displayed.
<tr> This tag begins a row in the table.

<td> This tag defines a column in the table, and stands for table data.

<th> This tag defines a table header. All text within this tag will be displayed in bold.

align = “right” This attribute is used below in the <td> tag to align the data displayed to the right of the column.

<table border=”1″>
<tr>
<th>Grade</th>
<th>Student Count</th>
</tr>
<tr>
<td>A</td>
<td align=”right”>10</td>
</tr>
<tr>
<td>B</td>
<td align=”right”>11</td>
</tr>
<tr>
<td>C</td>
<td align=”right”>7</td>
</tr>
<tr>
<td>D</td>
<td align=”right”>3</td>
</tr>
<tr>
<td>F</td>
<td align=”right”>2</td>
</tr>
</table>

The code above produces the following table:

Grade Student Count
A 10
B 11
C 7
D 3
F 2
Tagged with: , , , , , , , ,
Posted in HTML Examples

Create a Table From a SQL Statement

You can create a copy of a table using the SELECT INTO command.

SELECT * INTO TABLE2 FROM TABLE1

You can also join two or more tables and create a new table from the joined tables.

SELECT * INTO TABLE3 FROM TABLE1 a LEFT OUTER JOIN TABLE2 b ON a.field1 = b.field5

The newly created table will not have a primary key even if the existing table or tables do have a primary key.

Tagged with: , , , , , ,
Posted in SQL Server

SQL Server DatePart Example


select datepart(quarter,getdate())
–Get Week Day Numerical value, Sunday = 1st day of week
SELECT


 


CASE WHEN datepart(weekday,getdate()) = 1 THEN ‘Sunday’
WHEN datepart(weekday,getdate()) = 2 THEN ‘Monday’
WHEN datepart(weekday,getdate()) = 3 THEN ‘Tuesday’
WHEN datepart(weekday,getdate()) = 4 THEN ‘Wednesday’
WHEN datepart(weekday,getdate()) = 5 THEN ‘Thursday’
WHEN datepart(weekday,getdate()) = 6 THEN ‘Friday’
WHEN datepart(weekday,getdate()) = 7 THEN ‘Saturday’END


DECLARE @Date1 datetime


SET @Date1 = ’01/01/2020′


SELECT


CASE WHEN datepart(weekday,@Date1) = 1 THEN ‘Sunday’
WHEN datepart(weekday,@Date1) = 2 THEN ‘Monday’
WHEN datepart(weekday,@Date1) = 3 THEN ‘Tuesday’
WHEN datepart(weekday,@Date1) = 4 THEN ‘Wednesday’
WHEN datepart(weekday,@Date1) = 5 THEN ‘Thursday’
WHEN datepart(weekday,@Date1) = 6 THEN ‘Friday’
WHEN datepart(weekday,@Date1) = 7 THEN ‘Saturday’END


–Date Format DDD, MMM, DD, YYYY
DECLARE @Date2 datetime


SET @Date2 = ’01/01/2020′


SELECT


CASE WHEN datepart(weekday,@Date2) = 1 THEN ‘Sun’
WHEN datepart(weekday,@Date2) = 2 THEN ‘Mon’
WHEN datepart(weekday,@Date2) = 3 THEN ‘Tue’
WHEN datepart(weekday,@Date2) = 4 THEN ‘Wed’
WHEN datepart(weekday,@Date2) = 5 THEN ‘Thu’
WHEN datepart(weekday,@Date2) = 6 THEN ‘Fri’
WHEN datepart(weekday,@Date2) = 7 THEN ‘Sat’END
+ ‘, ‘ + convert(nvarchar(3),@Date2, 107) + ‘, ‘ + convert(nvarchar(2),@Date2,113) + ‘, ‘ + convert(nvarchar(4),year(@Date2))


select datepart(month,getdate())


select datepart(day,getdate())


select datepart(year,getdate())


select datepart(week,getdate())


select datepart(weekday,getdate())


select datepart(hour,getdate())


select datepart(minute,getdate())


select datepart(second,getdate())


select datepart(millisecond,getdate())


Tagged with: , , , ,
Posted in SQL Server

Facebook Tip – View Photos The ‘Old’ Way (Without the Pop Up)

Facebook recently changed the way you view photos.  By default, when you click on a photo, the photo pop ups over the Facebook page.  Previously, when you clicked on a photo, you would be directed to another Facebook page with the photo displayed on the page and previous and next links above the photo.

If you preferred the old way Facebook displayed photos, do the following:

Right Click on the picture.
Select Open link in new tab with the left mouse button.

That is it, you can now view photos without the pop up.

Tagged with: , , , ,
Posted in Facebook Tips

SQL Server DateDiff Function

–Date Difference Function
–Number of days between two dates
select datediff(day,getdate(),’09/01/2020′)

–Number of months between two dates
select datediff(month,getdate(),’09/01/2020′)

–Number of years between two dates
select datediff(year,getdate(),’09/01/2020′)


Tagged with: , , , , ,
Posted in SQL Server