Pages

Search This Blog

Saturday 24 May 2014

How to store values into database tables from your application....

Hi friends, in this session I will show you “How to store values from your page into database”

Step1:
You must create a table in the database.

Step2:
Connect the database in to your application. (See class 2)

Step3:

Let us take an example and see how to store data from our application into database table.

Registration form consists
·        Username
·        Password
·        Email

These 3 values must be stored into the database table.

Registration.aspx:

<asp:Label ID="lb1" runat="server" Text="Username "> </asp:Label>
<asp:TextBox ID="tbuser" runat="server"></asp:TextBox>

<asp:Label ID="lb1" runat="server" Text="Enter Password"> </asp:Label>
<asp:TextBox ID="tbpwd" runat="server"></asp:TextBox>

<asp:Label ID="lb1" runat="server" Text="Email"> </asp:Label>
<asp:TextBox ID="tbemail" runat="server"></asp:TextBox>

Registration.aspx.cs:

We can store data into database in two ways :

Way1: Creating a procedure in database. By using that procedure we will store data into database table.

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

public partial class Register : System.Web.UI.Page
{
 //ServerName  
 string str = "Data Source=SANTOSHKLPKL-HP\\SQLEXPRESS;Initial Catalog=blood_bank;Integrated Security=True";
//Creating a connection
    SqlConnection con = new SqlConnection();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
      
        con.ConnectionString = str;

  //Procedure name sp_insert where I wrote insert command
        SqlCommand cmd = new SqlCommand("sp_insert", con);
        cmd.CommandType = CommandType.StoredProcedure;

 //Initializing parameters values
       SqlParameter p1 = new SqlParameter("@User",tbuser.Text.Trim());
       SqlParameter p2 = new SqlParameter("@pwd",tbpwd.Text.Trim());
       SqlParameter p3 = new SqlParameter("@mail",tbemail.Text.Trim());
       
   // Adding paramaters into procedure so it can use it.
        cmd.Parameters.Add(p1);
        cmd.Parameters.Add(p2);
        cmd.Parameters.Add(p3);
   
       con.Open();
       
       cmd.ExecuteNonQuery();
              
      Response.Write("<script> alert('User Registered successfully
         !')</script>");
                       
       }                    
    
}

Way2:

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

public partial class Register : System.Web.UI.Page
{

//ServerName  
 string str = "Data Source=SANTOSHKLPKL-HP\\SQLEXPRESS;Initial 
Catalog=blood_bank;Integrated Security=True";

   //Creating a connection
   SqlConnection con = new SqlConnection();
   
   protected void Page_Load(object sender, EventArgs e)
    {    
    }

    protected void btnsubmit_Click(object sender, EventArgs e)
    {
       
        con.ConnectionString = str;

     // Storing textbox values into strings   

       string s1,s2,s3;
       s1=tbuser.Text;
       s2=tbpwd.Text;
       s3=tbemail.Text;
    
    // I wrote insert command here only. Uname,Password,Email are       database table column names.
     SqlCommand cmd = new SqlCommand("Insert into registration               (Uname,Password,Email)  values (‘s1’,’s2’,’s3’)”, con);
      
       con.Open();
       cmd.ExecuteNonQuery();       

       Response.Write("<script> alert('User Registered successfully
         !')</script>");
                
       }                    
}  

You can rate my class on right side top of my blog.....

                                                                        -Your's santoshklpkl

No comments:

Post a Comment

Animated Social Gadget - Blogger And Wordpress Tips Twitter Bird Gadget