Tuesday 31 March 2020

how to connect sql server database using visual studio with asp.net and C#.

Asp.Net code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Registration.aspx.cs" Inherits="dbconnect_web.dbconnect" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h4>Register page</h4>
            <table>
                <tr>
                    <td><asp:Label ID="username" runat="server" Text="User Name :"></asp:Label></td>
                    <td><asp:TextBox ID="input1" runat="server"></asp:TextBox></td>
                    <asp:RequiredFieldValidator ID="validate" runat="server" ControlToValidate="input1" ErrorMessage="Enter your user name" EnableClientScript="false"></asp:RequiredFieldValidator>
                    </tr>
                <tr>
                    <td><asp:Label ID="password" runat="server" Text="Password :"></asp:Label></td>
                    <td><asp:TextBox ID="input2" runat="server" TextMode="Password"></asp:TextBox></td>                 
                </tr>
                <tr>
                <td><asp:Label ID="mobile" runat="server" Text="mobile :"></asp:Label></td>
                    <td><asp:TextBox ID="input3" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                <td><asp:Label ID="email" runat="server" Text="email :"></asp:Label></td>
                    <td><asp:TextBox ID="input4" runat="server"></asp:TextBox></td>
                    </tr>
                <tr>
                    <td> <asp:Button ID="Button1" runat="server" text="Register" OnClick="button1_click" ForeColor="#66FF66" /> </td>
                </tr>
                <tr>
                    <td><asp:Label ID="label1" runat="server"></asp:Label></td>
                </tr>
                <tr>
                    <td><asp:Label ID="label2" runat="server"></asp:Label></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>


C# Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.SqlClient;


namespace dbconnect_web
{
    public partial class dbconnect : System.Web.UI.Page
    {
        public static string Encryptdata(string password)
        {
            byte[] b = System.Text.ASCIIEncoding.ASCII.GetBytes(password);
            string encrypted = Convert.ToBase64String(b);
            return encrypted;
        }

        public string DecryptString(string password)
        {
            byte[] b;
            string decrypted;
            try
            {
                b = Convert.FromBase64String(password);
                decrypted = System.Text.ASCIIEncoding.ASCII.GetString(b);
            }
            catch (FormatException fe)
            {
                decrypted = "";
            }
            return decrypted;
        }
        protected void button1_click(object sender, EventArgs e)
        {
            try
            {
                // Build connection string
                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
                builder.DataSource = "localhost\\SQL";
                builder.UserID = "sa";
                builder.Password = "admin";
                builder.InitialCatalog = "AdventureWorks2012";

                // Connect to SQL
                Console.Write("Connecting to SQL Server ... ");
                using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
                {
                    connection.Open();
                    string sql = "insert into Users values (@username,@password,@mobile,@email);";
                    SqlCommand cmd = new SqlCommand(sql, connection);
                    string user = input1.Text;
                    string password = input2.Text;                
                    string mobile= input3.Text;
                    string email = input4.Text;
                    cmd.Parameters.AddWithValue("@username", user);
                    cmd.Parameters.AddWithValue("@password", Encryptdata(password));
                    cmd.Parameters.AddWithValue("@mobile", mobile);
                    cmd.Parameters.AddWithValue("@email", email);
                    int rowsAffected = cmd.ExecuteNonQuery();
                    label1.Text = "Registration complted";
                }

            }
            catch (SqlException error)
            {
                label2.Text = error.Message;
            }

        }
    }
    
}