Posts

Showing posts with the label ASPNET

Enable MVC On ASP.NET Core Application

Image
In this post we will go over the process of enabling ASP.NET MVC in our application.  Just like static files, in order for us to use MVC in our application we have to tell ASP.NET Core to use in the Startup.cs file.  We will continue to use the application "NorthwindCafe" that we used in our previous blog. Here are the steps to add MVC to your application: 1.  Open the Startup.cs file, then in "ConfigureServices" method type in the following to enable MVC public void ConfigureServices(IServiceCollection services) { services.AddMvc(); } 2. As with the static files, there will be a red underline on the .AddMvc() method that's because we haven't added the package to your project yet.  So click on the yellow light and select the first option to add Microsoft.AspNET.Mvc package to our project. 3.  Now go into the Configure method and type app.UseMvc() into the method, the final markup should look like the following publ...

Bind Enum Type to A DropDownList Control In ASP.NET C#

Image
Suppose you have an enum type like the one below, and you want to bind the enum type to a DropDownList control in a Web Forms application.  How would you do this?  There's an easy way to do this with just a few lines of code.  You'll be amaze at how simple it is. First of all here is our markup code in our .aspx page <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Sandbox.Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList> </div> </form> </body> </html> Now let's define our enum type, the last value is 400 just to confirm that the Dr...

ASP.NET GridView Control Part 1: Getting Started, Create GridView in Web Form

Image
The GridView data grid control is probably the most popular and flexible data grid control in the ASP.NET arsenal.  In this tutorial we will create a new GridView data grid on a web form. Here are the steps: 1. Click on the "Design" tab in main window of Visual Studio 2. Click on the "Toolbox" tab, then expand the "Data" node, then drag the "GridView" data control to the Design surface.  The Design Surface is the big window in the middle of Visual Studio. 3. Click on "Toolbox" tab again, this time drag the "SqlDataSource" control to the Design Surface 4. Click on the ">" button next to SqlDataSource1 control, then select "Configure Data Source" 5. Click on the "New Connection" button  6. Select your "Server name", SQL Server should pick up your database server automatically if it's on a local machine, but if it doesn't type in (local) in the "Server name" field.   7....

ASP.NET: Getting The Inserted ID Back With Scope_Identity()

When you need to do an insert into multiple database table you need to the get the ID of the insert so that you could use that ID for the next insert. Here is how you would do that with the Scope_Identity()which gets the last inserted ID back to you if you execute your query with the ExecuteScalar() method. SqlCommand cmd = new SqlCommand("INSERT INTO Users (" + "LoginName," + "FirstName," + "LastName," + "Password," + "Email," + "DOB," + "Sex" + ") VALUES (" + "@Email," + "@FirstName," + "@LastName," + "@Password," + "@Email," + "@DOB,"...

SqlDbType.Date

string dobStr = ddlMonth.SelectedValue + "/" + txtDay.Text + "/" + txtYear.Text; SqlParameter dob = new SqlParameter("DOB", Convert.ToDateTime(dobStr)); dob.SqlDbType = SqlDbType.Date; cmd.Parameters.Add(dob);

ASP.NET Get The Current Page File Name

In this blog we will get the current page file name that the user is currently on. string[] currentUrl = HttpContext.Current.Request.Url.AbsolutePath.Split('/'); string pageFileName = currentUrl[currentUrl.Length-1];

ADO.NET: Using System.Data.SqlClient.SqlDataAdapter To Fill System.Data.DataTable

In this example we will use the SqlDataAdapter to fill a DataTable then bind the data to a GridView  1. Drag a GridView into your ASP.NET page 2. Use the following code to the fill the dtProducts DataTable with the adapter SqlDataAdapter, then bind it to the GridView1 control. protected void Page_Load(object sender, EventArgs e) { DataTable dtProducts = new DataTable(); string connString = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"]. ConnectionString; using (SqlConnection conn = new SqlConnection(connString)) { SqlCommand cmd = new SqlCommand("SELECT * FROM Products", conn); conn.Open(); SqlDataAdapter adapter = new SqlDataAdapter(cmd); adapter.Fill(dtProducts); GridView1.DataSource = dtProducts; GridView1.DataBind(); } }

ASP.NET Populate The DropDownList Control With The Northwind Categories Table

Image
The Categories table is a perfect example of how sometimes you have to populate the DropDownList control to data from the database. In this example we will populate the DropDownList control to the Categories table in the Northwind database.  1. Drag the DropDownList control into a .aspx page. Make sure you check "Enable AutoPostBack" 2.  In your C# code file you need the namespaces using System.Web.Configuration; using System.Data.SqlClient; using System.Data; 2. Then get the Northwind connection string value from the Web.config file string connectString = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"]. ConnectionString; 3. Type in the following code in the Page_Load method protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { using (SqlConnection conn = new SqlConnection(connectString)) { SqlCommand cmd = new SqlCommand(...

ASP.NET: Programmatically Connect to The Database Using SqlConnection, DataReader and SqlCommand

So you have following connection string to the Northwind database in your Web.config and you want to connect to the database in the code behind instead using query builder. <connectionStrings> <add name="NorthwindConnectionString" connectionString="Data Source=(local); Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data. SqlClient"/></connectionStrings> Here is how you would do it 1. Get the connection from the Web.config file programmatically, you can look at this blog to find out how perform this step by step.  But here is the code to get the connection string from the Web.config file string connectString = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"]. ConnectionString; 2.  On the top of the file specify that you want to use the Sql.Data.SqlClient namespace by typing using System.Data.SqlClient; 3. Now type the following using (SqlConnection conn = new SqlConnection(...

ASP.NET: Set Up SQL Server for ASP.NET Provider Databases

Image
Out of the box the ASP.NET provider databases works with the local database file call aspnetdb.mdf, however in a production environment you cannot use this file because you need to work with the production database. To create the provider databases on a full version of SQL Server perform the following steps. 1. Navigate to the .NET Framework version that you want to use. Version 4.5 and Version 2 has the provider database wizard. I am using version 4.5 so the path is C:\Windows\Microsoft.NET\Framework\v4.0.30319 2. Double click on the file aspnet_regsql.exe, the ASP.NET SQL Server Setup Wizard will apear, click "Next" to continue 3.  Select "Configure SQL Server for application services" radio button, then click next 4. Type in the database server name in the "Server:" field.  Make sure you type in the actual host name because if you type in (local) or localhost the database creation process will fail.   Leave the option as "Windows authentication...

ASP.NET Configurations: Retrieve Multiple Connection Strings From The Current Web Application's Web.config File

So you have to work with multiple connection strings that you want to use in the Web.config file and you want to use it in the code behind file.    Below are the steps to retrieve multiple connection strings in the web.config file programmatically. 1. Make sure you have connection strings in your Web.config file.  A connection string looks something like this. <connectionStrings> <add name="NorthwindConnectionString" connectionString="Data Source=(local); Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient" /> <add name="AdventureWorksConnectionString" connectionString="Data Source=(local); Initial Catalog=AdventureWorks;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> 2. Now in the Default.aspx.cs file type in the following in the top of the file, where all the using statements are to use the System.Web.Co...

ASP.NET Configurations: Retrieve Connection String Programmatically

So you have a connection string that you want to use in the Web.config file and you want to use in the code behind file.  You can do that with a single line of code in your .cs file.  Below are the steps to retrieve a connection string in the web.config file programmatically. 1. Make sure you have a connection string in your Web.config file.  A connection string looks something like this. <connectionStrings> <add name="NorthwindConnectionString" connectionString="Data Source=(local); Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient"/> </connectionStrings> 2. Now in the Default.aspx.cs file type in the following in the top of the file, where all the using statements are to use the System.Web.Configuration namespace using System.Web.Configuration; Obviously you can use it in any .cs file, the Default.aspx.cs is just an example. 3. In the Page_Load method type in the following line string co...

ADO.NET: SqlDataSource Control

Image
The SqlDataSource control provides you with a quick and easy way to access database data.  Most of the time you don't even have to write a single line of code.  Now this can a be a good thing and a bad thing, depends on how you look at things.  Most of the configuration and querying is performed via a configuration wizard. Here are the steps to creating a SqlDataSource: 1. In a ASP.NET project/web site drag a SqlDataSource control in under "Data" in the "Toolbox" tab into a .aspx page.   After you dragged the control it should look like this on your .aspx page 2. Click on "SqlDataSource1" control, then click on the ">" sign, a slide out menu appears.  Select "Configure Data Source". The "Configure Data Source" wizard will pop up. 3. Click on the "New Connection" button, the "Add Connection" wizard will pop up. Select or type in your "Server name".  If you have a local instance of SQL Server...

Entity Framework Part 1: Generate an Entity Framework for the Northwind Database Tables

Image
In my previous post we've created the Northwind products page using the SqlDataSource data control, even though there's nothing wrong with the solution.  SqlDataSource is a an old technology and is not meant to be an enterprise solution.  SqlDataSource is the stone age of .NET data access technology.  The future of .NET is the Entity Framework. The goal of the Entity Framework is to create a conceptual model of your data store so that you can work with tables and rows as objects, or entities.  In essence the developer does not know or care what the data store is, he does not have to be a dba to work with the data. To demonstrate the Entity Framework let's create the Northwind products page using the Entity Data Model of the Northwind database. Here are the steps: 1. Open the Northwind project you created with the SqlDataSource 2. Righ-click on the Northwind project in "Solution Explorer", then select "Add", then "New Item" 3. Select "Data...