Tuesday, July 16, 2013

Primary and Foregn Key data save in database

1)Open Visual Studio 2010>>>File....New...Website
aspx file design are as below :-
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="PramaryandForegn._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            height: 21px;
        }
        .style3
        {
            width: 318px;
        }
        .style4
        {
            height: 21px;
            width: 318px;
        }
    </style>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

    <table align="center" class="style1">
        <tr>
            <td class="style3">
                <asp:Label ID="Label1" runat="server" Text="Username"></asp:Label>
              </td>
            <td>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </td>
            <td>
               </td>
        </tr>
        <tr>
            <td class="style4">
                <asp:Label ID="Label2" runat="server" Text="password "></asp:Label>
              </td>
            <td class="style2">
                <asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
            </td>
            <td class="style2">
               </td>
        </tr>
        <tr>
            <td class="style3">
                <asp:Label ID="Label3" runat="server" Text="Name"></asp:Label>
              </td>
            <td>
                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            </td>
            <td>
               </td>
        </tr>

        <tr>
            <td class="style3">
                <asp:Label ID="Label4" runat="server" Text="Lastname"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style3">
                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
            </td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
    </table>

 </asp:Content>

I am Using sql Server r2

Create One Database(Mt database name is kazi)

Create 2 table(I am Create table  Login_table and table Primary_table)

Table one:-


table 2


Store procedure:-SpInsert


Know time to code in .CS file the code are as below:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace PramaryandForegn

{
    public partial class _Default : System.Web.UI.Page
    {
        SqlConnection connection;
        SqlCommand command;
        string strsqlcon = ConfigurationManager.ConnectionStrings["sqlconn"].ConnectionString.ToString();
       
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            connection = new SqlConnection(strsqlcon);
            command = new SqlCommand("SpInsert", connection);
            command.CommandType = CommandType.StoredProcedure;
            command .Parameters .AddWithValue ("@UserName",TextBox1 .Text );
             command .Parameters .AddWithValue ("@Password",TextBox2 .Text );
             command .Parameters .AddWithValue ("@Name",TextBox3 .Text );
             command .Parameters .AddWithValue ("@LastName",TextBox4 .Text );
            SqlParameter output=new SqlParameter ("@out",SqlDbType .Int );
           output .Direction=ParameterDirection .Output ;
            command .Parameters .Add(output );
            connection.Open();
            command.ExecuteNonQuery();
            int outval = (int)output.Value; 
            connection.Close();
            if (outval == 1)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('success');", true);
            }

        }
    }
}


Know RUN the program press F5

when program run successfully the below screen Came:-
 


 Know fill the field as below or what u want :-



Click on submit Button when u click in submit button the screen as below :-




Click on OK

Know goto Sql Server and write a query fro cross check  the query and screen are as below :-



So this is final result Thank you for read my blog

No comments:

Post a Comment