Progress Monitor for IdealProgrammer.com
Take Swift, Intelligent, Massive, Planned, Loving, Effective (S.I.M.P.L.E.) Action to Transform Self into Ideal
Skip Repetitive Navigational Links
Home
News
Presenters
Register
Contact
Categories
Titles
Converter
Code Samples
C# ASP.NET
C# Console
HTML
JavaScript
SQL Server
VB ASP.NET
VB Console
Please login
C# ASP.NET Sql Command Select Statement Source Code Example
Description:
Illustrates using Sql Command Select Statement in C-Sharp ASP.NET.
Example Webform Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SqlCommandSelect.aspx.cs" Inherits="Database_ADONET_SqlCommandSelect" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Sql Command Select C-Sharp ASP.NET</title> </head> <body> <form id="Form1" runat="server"> <p> Find: <asp:TextBox id="TextBox1" runat="server" Text="*"></asp:TextBox> <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Go"></asp:Button> </p> <p> <asp:DataGrid id="DataGrid1" runat="server" BorderWidth="5px" BorderColor="Black" CellPadding="5"> <HeaderStyle font-size="X-Small" font-names="Arial Narrow" font-bold="True" forecolor="White" backcolor="Black"></HeaderStyle> <AlternatingItemStyle backcolor="Silver"></AlternatingItemStyle> <ItemStyle font-size="X-Small" font-names="Arial Narrow"></ItemStyle> </asp:DataGrid> </p> </form> </body> </html>
Example Code Behind:
using System; using System.Data; using System.Data.SqlClient; using System.Configuration; partial class Database_ADONET_SqlCommandSelect : System.Web.UI.Page { System.Data.DataSet FindByTitle(string search) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Pubs_ConnectionString"].ConnectionString); string queryString = ""; if (search == "*") { queryString = "SELECT [titles].[title], [titles].[price], [titles].[notes], [titles].[pubdate] FROM [titles]"; } else { queryString = "SELECT [titles].[title], [titles].[price], [titles].[notes], [titles].[pubdate] FROM [titles] WHERE ([titles].[title] like @search + '%')"; } System.Data.SqlClient.SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand(queryString, con); if (search != "*") { sqlCmd.Parameters.Add("@search", System.Data.SqlDbType.NVarChar).Value = search; } System.Data.SqlClient.SqlDataAdapter dataAdapter = new System.Data.SqlClient.SqlDataAdapter(sqlCmd); System.Data.DataSet dataSet = new System.Data.DataSet(); dataAdapter.Fill(dataSet); return dataSet; } public void Button1_Click(object sender, EventArgs e) { string sTitle = TextBox1.Text; DataGrid1.DataSource = FindByTitle(sTitle); DataGrid1.DataBind(); } }
Example Connection String:
<connectionStrings> <add name="Nortwind_ConnectionString" connectionString="Server=(local)\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI" /> <add name="Pubs_ConnectionString" connectionString="Server=(local)\SQLEXPRESS;Initial Catalog=pubs;Integrated Security=SSPI" /> </connectionStrings>
Home
News
Presenters
Register
Contact
Categories
Titles
Converter
Code Samples
C# ASP.NET
C# Console
HTML
JavaScript
SQL Server
VB ASP.NET
VB Console