How To: Add A Default Listitem To A Dropdownlist: Appenddatabounditems
How To: Add A Default Listitem To A Dropdownlist: Appenddatabounditems
How To: Add A Default Listitem To A Dropdownlist: Appenddatabounditems
Aug 04, 2007 05:34 PM|LINK This can be done one of 2 ways. A default ListItem can be added to a DropDownList programmatically with the following syntax after binding data to the DropDownList: //Code here to populate DropDownList DropDownListID.Items.Insert(0, new ListItem("Default text", "Default value") This will add a ListItem to index 0, which will be the first ListItem. In .NET 2.0, this can be done declaratively using the AppendDataBoundItems property. This will append all data-bound ListItems to the DropDownList, leaving those you add manually as the first selections. <asp:DropDownList ID="DropDownListID" AppendDataBoundItems="true" runat="server"> <asp:ListItem Text="Default text" Value="Default value" /> </asp:DropDownList>
.
COALESCE
http://msdn.microsoft.com/en-us/library/aa258244%28v=sql.80%29.aspx
Syntax
COALESCE ( expression [ ,...n ] )
Arguments
Is a placeholder indicating that multiple expressions can be specified. All expressions must be of the same type or must be implicitly convertible to the same type.
Return Types
If all arguments are NULL, COALESCE returns NULL. COALESCE(expression1,...n) is equivalent to this CASE function:
CASE WHEN (expression1 IS NOT NULL) THEN expression1 ... WHEN (expressionN IS NOT NULL) THEN expressionN ELSE NULL
Examples
In this example, the wages table is shown to include three columns with information about an employee's yearly wage: hourly_wage, salary, and commission. However, an employee receives only one type of pay. To determine the total amount paid to all employees, use the COALESCE function to receive only the nonnull value found in hourly_wage, salary, and commission.
SET NOCOUNT ON GO USE master IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'wages') DROP TABLE wages GO CREATE TABLE wages ( emp_id tinyint identity, hourly_wage decimal NULL, salary decimal NULL, commission decimal NULL, num_sales tinyint NULL ) GO INSERT wages VALUES(10.00, NULL, NULL, NULL) INSERT wages VALUES(20.00, NULL, NULL, NULL) INSERT wages VALUES(30.00, NULL, NULL, NULL) INSERT wages VALUES(40.00, NULL, NULL, NULL) INSERT wages VALUES(NULL, 10000.00, NULL, NULL) INSERT wages VALUES(NULL, 20000.00, NULL, NULL) INSERT wages VALUES(NULL, 30000.00, NULL, NULL) INSERT wages VALUES(NULL, 40000.00, NULL, NULL) INSERT wages VALUES(NULL, NULL, 15000, 3)
INSERT wages VALUES(NULL, NULL, 25000, 2) INSERT wages VALUES(NULL, NULL, 20000, 6) INSERT wages VALUES(NULL, NULL, 14000, 4) GO SET NOCOUNT OFF GO SELECT CAST(COALESCE(hourly_wage * 40 * 52, salary, commission * num_sales) AS money) AS 'Total Salary' FROM wages GO
..
DataColumn col5 = new DataColumn("Price"); DataColumn col6 = new DataColumn("Brand"); DataColumn col7 = new DataColumn("Remarks"); Define DataType of the Columns col1.DataType = System.Type.GetType("System.Int"); col2.DataType = System.Type.GetType("System.String"); col3.DataType = System.Type.GetType("System.Boolean"); col4.DataType = System.Type.GetType("System.String"); col5.DataType = System.Type.GetType("System.Double"); col6.DataType = System.Type.GetType("System.String"); col7.DataType = System.Type.GetType("System.String"); Add All These Columns into DataTable table table.Columns.Add(col1); table.Columns.Add(col2); table.Columns.Add(col3); table.Columns.Add(col4); table.Columns.Add(col5); table.Columns.Add(col6); table.Columns.Add(col7); Create a Row in the DataTable table DataRow row = table.NewRow(); Fill All Columns with Data row[col1] = 1100; row[col2] = "Computer Set"; row[col3] = true; row[col4] = "New computer set"; row[col5] = 32000.00 row[col6] = "NEW BRAND-1100"; row[col7] = "Purchased on July 30,2008"; Add the Row into DataTable table.Rows.Add(row); Want to bind this DataTable to a GridView? GridView gvTest=new GridView(); gvTest.DataSource = table; gvTest.DataBind();
extremely handy when it comes to binding it to a control like a GridView. So to make use of both the DataReader and DataTable in the same solution, we can fetch the data using a DataReader and then convert it to a DataTable and bind it to the control. In this article, we will explore how to do the conversion using two approaches; the first one, a direct method by using the DataTable.Load() and the second one, by manually converting a DataReader to a DataTable. Step 1: Create a new ASP.NET application. Drag and drop two GridView controls to the page. We will fetch data from a DataReader into a DataTable and bind the DataTable to the GridViews. Before moving ahead, add a web.config file to the project and add the following element. <connectionStrings> <addname="NorthwindConn"connectionString="Data Source=(local); Initial Catalog=Northwind; Integrated Security=true;"/> </connectionStrings> Step 2: Let us first see how to convert a DataReader to a DataTable using the easy way out. DataTable in ADO.NET 2.0 contains a Load() method which enables the DataTable to be filled using a IDataReader. This method is quiet handy when you need to quickly create a DataTable, without using a DataAdapter!! Let us see how. C# private void ConvertDateReadertoTableUsingLoad() { SqlConnection conn = null; try { string connString = ConfigurationManager.ConnectionStrings["NorthwindConn"].ConnectionString; conn = new SqlConnection(connString); string query = "SELECT * FROM Customers"; SqlCommand cmd = new SqlCommand(query, conn); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); DataTable dt = new DataTable(); dt.Load(dr); GridView1.DataSource = dt; GridView1.DataBind(); } catch (SqlException ex) { // handle error } catch (Exception ex) { // handle error } finally { conn.Close(); } }