How To Connect Web Page To Sql Database In ASP.NET?

Your SQL database should be connected to SharePoint. Here are the steps needed to run NET using C#. To open Visual Studio 2015 Update 3, go to the File > New > Project menu, or click on “Ctrl+Shift+N”. To create an ASP server for Visual C#, select nsite and undise Visual C# >> Web >> ASP. This Web Application belongs to the NET. A final click of the “OK” button will complete the process.

How Does Web Form Connect To Sql Server Database?

Choose your ASP software >>-new Project option. Web Applications, for both NET and.NET 2.0 are completely empty. You may select Connecting to Database under Tools in the top right corner after selecting the new project. The Add Connection dialogue box will appear, where you can click the new option and choose the Microsoft SQL Server setting. The server name will now appear on your screen.

Net Connect To Sql Server?

After that, the ASP will be created. Membership databases and school databases belonging to Net. Using Visual Web Developer, right-click Data Connection and select Create New SQL Server Database from the View menu; then select Server Explorer (Database Explorer). Click “Create New SQL Server Database” on the Create New SQL Server Database dialog box.

If you want to connect to the SQL database into ASP.NET, using C#, it should follow the steps given below.

Step 1


Now, Open Visual Studio 2015 Update 3, go to the File >> New >> Project or use the shortcut key “Ctrl+Shift +N”.
Step 2

Here, select Visual C# >> Web >> ASP.NET Web Application. Finally, click “OK” button.

Step 3

Here, you can select the template for your ASP.NET Application. We are choosing “Empty” here. Now, click OK button.
Step 4

Now, open the project and look for the Solution Explorer.
Here, open the default .aspx. If you want a Webform, you can add the page (Web Form). Add+New item (Ctrl+Shift+A).
Now, we can create a login page, using ASP.NET code. You need to follow the drag and drop method. Here, we already created the login page.


Step 5
Now, open the project. If you want a SQL Server database, you can add the page (SQL Server database). Add+New item (Ctrl+Shift+A).

Here, you can select Visual C# and choose SQL Server database. Afterwards, click “OK” button.
Here, open the new Window and click YES button.
Now, add this to the database in our project.
Step 6
Now, we can go to the Server Explorer and add your database. You can click the Tables and afterwards, click Add New Table.
Now, open the new table and you can fill the data, which is like (studentname, password) and afterwards, you click the Update .
Here, click database in update and subsequently click update the database.

Here, the database is updated.

Here, the database and data are added.

Now, we can click  on the right click and click Show Table Data.
Now, the data is stored.
Step 7
Now, you can add SQL Data Source. Drag and drop method. Here, click Configure Data Source
Now, we can choose your database and click NEXT button.

Now, you can select the ConnectionString and Click NEXT button

Now, we can choose Specify columns from a table or view and afterwards, click Next button.

Now, click Test Query.
Here, add the data and click Finish button.
Step 8
Now, you can go to CS(C# code) page and you will write the C# code.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data.SqlClient;
  8. using System.Configuration;
  9. namespace DatabaseConnectivity
  10. {
  11.     public partial class loginpage  System.Web.UI.Page
  12.     {
  13.         protected void Page_Load(object sender, EventArgs e)
  14.         {
  15.            if(IsPostBack)
  16.             {
  17.                 SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[“RegiConnectionString”].ConnectionString);
  18.                 conn.Open();
  19.                 string checkuser = “select count(*) from RegisterDataBase where StudentName='”+TextBox1.Text+“‘”;
  20.                 SqlCommand cmd = new SqlCommand(checkuser, conn);
  21.                 int temp = Convert.ToInt32(cmd.ExecuteScalar().ToString());
  22.                 if (temp == 1)
  23.                 {
  24.                     Response.Write(“Student Already Exist”);
  25.                 }
  26.                 conn.Close();
  27.             }
  28.             }
  29.         protected void Button1_Click(object sender, EventArgs e)
  30.         {
  31.             try
  32.             {
  33.                 SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[“RegiConnectionString”].ConnectionString);
  34.                 conn.Open();
  35.                 string insertQuery = “insert into RegisterDataBase(StudentName,Passwords,EmailId,Department,College)values (@studentname,@passwords,@emailid,@department,@college)”;
  36.                 SqlCommand cmd = new SqlCommand(insertQuery, conn);
  37.                 cmd.Parameters.AddWithValue(“@studentname”, TextBox1.Text);
  38.                 cmd.Parameters.AddWithValue(“@passwords”, TextBox2.Text);
  39.                 cmd.Parameters.AddWithValue(“@emailid”, TextBox3.Text);
  40.                 cmd.Parameters.AddWithValue(“@department”, TextBox4.Text);
  41.                 cmd.Parameters.AddWithValue(“@college”, TextBox5.Text);
  42.                 cmd.ExecuteNonQuery();
  43.                 Response.Write(“Student registeration Successfully!!!thank you”);
  44.                 conn.Close();
  45.             }
  46.             catch (Exception ex)
  47.             {
  48.                 Response.Write(“error” + ex.ToString());
  49.             }
  50.         }
  51.     }
  52. }

Now, you can see the Loginpage.aspx code.

  1. <%@ Page Language=“C#” AutoEventWireup=“true” CodeBehind=“loginpage.aspx.cs” Inherits=“DatabaseConnectivity.loginpage” %>
  2. <!DOCTYPE html>
  3. <html xmlns=“http//www.w3.org/1999/xhtml”>
  4. <head runat=“server”>
  5.     <title></title>
  6.     <link href=“stylepage.css” type=“text/css” rel=“stylesheet” />
  7.     <style type=“text/css”>
  8.         .auto-style1 {
  9.             width 100%;
  10.         }
  11.     </style>
  12. </head>
  13. <body>
  14.     <form id=“form1” runat=“server”>
  15.     <div id=“title”>
  16.     <h1>REGISTER PAGE</h1>
  17.     </div>
  18.         <div id =“teble”></div>
  19.     <table class=“auto-style1”>
  20.         <tr>
  21.             <td>
  22.                 <aspLabel ID=“Label1” runat=“server” Text=“StudentName”></aspLabel></td>
  23.             <td>
  24.                 <aspTextBox ID=“TextBox1” runat=“server”></aspTextBox></td>
  25.         </tr>
  26.         <tr>
  27.             <td>
  28.                 <aspLabel ID=“Label2” runat=“server” Text=“Password”></aspLabel></td>
  29.             <td>
  30.                 <aspTextBox ID=“TextBox2” runat=“server”></aspTextBox></td>
  31.         </tr>
  32.         <tr>
  33.             <td>
  34.                 <aspLabel ID=“Label3” runat=“server” Text=“EmailId”></aspLabel></td>
  35.             <td>
  36.                 <aspTextBox ID=“TextBox3” runat=“server”></aspTextBox></td>
  37.         </tr>
  38.         <tr>
  39.             <td>
  40.                 <aspLabel ID=“Label4” runat=“server” Text=“Department”></aspLabel></td>
  41.             <td>
  42.                 <aspTextBox ID=“TextBox4” runat=“server”></aspTextBox></td>
  43.         </tr>
  44.         <tr>
  45.             <td>
  46.                 <aspLabel ID=“Label5” runat=“server” Text=“College”></aspLabel></td>
  47.             <td>
  48.                 <aspTextBox ID=“TextBox5” runat=“server”></aspTextBox></td>
  49.         </tr>
  50.     </table>
  51.     <div id=“button”>
  52.         <aspButton ID=“Button1” runat=“server” Text=“submit” OnClick=“Button1_Click” BackColor=“Yellow” />
  53.     </div>
  54.         <div id=“sim”></div>
  55.         <aspSqlDataSource ID=“SqlDataSource1” runat=“server” ConnectionString=“<%$ ConnectionStringsRegiConnectionString %>” SelectCommand=“SELECT * FROM [RegisterDataBase]”></aspSqlDataSource>
  56.         <div id=“grid”>
  57.             <aspGridView ID=“GridView1” runat=“server” AllowPaging=“True” AllowSorting=“True” AutoGenerateColumns=“False” CellPadding=“4” DataSourceID=“SqlDataSource1” ForeColor=“#333333” GridLines=“None”>
  58.                 <AlternatingRowStyle BackColor=“White” ForeColor=“#284775” />
  59.                 <Columns>
  60.                     <aspBoundField DataField=“Id” HeaderText=“Id” SortExpression=“Id” />
  61.                     <aspBoundField DataField=“StudentName” HeaderText=“StudentName” SortExpression=“StudentName” />
  62.                     <aspBoundField DataField=“Passwords” HeaderText=“Passwords” SortExpression=“Passwords” />
  63.                     <aspBoundField DataField=“EmailId” HeaderText=“EmailId” SortExpression=“EmailId” />
  64.                     <aspBoundField DataField=“Department” HeaderText=“Department” SortExpression=“Department” />
  65.                     <aspBoundField DataField=“College” HeaderText=“College” SortExpression=“College” />
  66.                 </Columns>
  67.                 <EditRowStyle BackColor=“#999999” />
  68.                 <FooterStyle BackColor=“#5D7B9D” -Bold=“True” ForeColor=“White” />
  69.                 <HeaderStyle BackColor=“#5D7B9D” -Bold=“True” ForeColor=“White” />
  70.                 <PagerStyle BackColor=“#284775” ForeColor=“White” HorizontalAlign=“Center” />
  71.                 <RowStyle BackColor=“#F7F6F3” ForeColor=“#333333” />
  72.                 <SelectedRowStyle BackColor=“#E2DED6” -Bold=“True” ForeColor=“#333333” />
  73.                 <SortedAscendingCellStyle BackColor=“#E9E7E2” />
  74.                 <SortedAscendingHeaderStyle BackColor=“#506C8C” />
  75.                 <SortedDescendingCellStyle BackColor=“#FFFDF8” />
  76.                 <SortedDescendingHeaderStyle BackColor=“#6F8DAE” />
  77.             </aspGridView>
  78.         </div>
  79.             <div id=“last”>
  80.             <h3>Developed by
  81.                       Muthuramalingam Duraipandi</h3>
  82.         </div>
  83.     </form>
  84.     </body>
  85. </html>
Step 9

Here, you need to run any Browser and after a few minutes, you will get some output. Now, we can insert the data into your database.

Step 10
Now, we can added the GridView. Drag and drop method needs to be used.
Now, you can choose the  data source, it is sqlDataSource.
Here, the database data is added to GridView.


Step 11
Here, you need to run any Browser and after a few minutes, you will get an output. Now, we can view the data.
Posted in Hosting Article.