An Effective Way To Store Some Values In Database In ASP.NET

In this tutorial, we will learn a way to store some specific data at one instance of button click in Datatable; and on another instance of button click, we will store some other data to our Datatable. The approach we are using here is something like- we had two Views. On first View, we will save the name and city, and on the other View, we will save course and semester in our same datatable.

ASP.NET

INITIAL CHAMBER

Step 1

Open your Visual Studio 2010 and create an Empty Website. Give a suitable name [data_demo].

Step 2

In Solution Explorer, you get your empty website. Add a web form and SQL Database by going like this –

For Web Form

data_demo (Your Empty Website) -> Right Click -> Add New Item -> Web Form. Name it as -> data_demo.aspx.

For SQL Server Database

data_demo (Your Empty Website) -> Right Click -> Add New Item -> SQL Server Database. [Add Database inside the App_Data_folder].

DATABASE CHAMBER

Step 3

Get to your Database [Database.mdf], we will create a table - -> tbl_data1. Go to the database.mdf - -> Table - -> Add New table, and design your table like this.

ASP.NET

Relevant Store Procedure - sp_insertdata 

  1. CREATE PROCEDURE sp_inserdata  
  2. (  
  3.   
  4.       
  5.     @id int output,  
  6.     @name varchar(50)= 'Nilesh',  
  7.     @city varchar(50)= 'Rajkot'  
  8. )  
  9. AS  
  10.   
  11. insert into tbl_data1 (name,city) values (@name,@city)  
  12.   
  13. select @id = id from tbl_data1 where name=@name and city=@city  
  14.   
  15. RETURN   

We cannot save all the data at one time, our requirement is to save two of the data fields at one instance, and other two at other instance on button click. Also, we want to save these 4 data fields in one single table. Even if you go with a traditional way of inserting, it will show you null values in your datatable.

With a traditional way, on the first save- course and sem will be shown as null values, but let’s say you don’t want to add these null values, you need to enter the default values, at that time I guess you will take another field in your view, yes it is a simple approach as we are here storing 4 datafields, but we are talking here for large data let’s say 100-200 at that time too if you add these fields than it is not a reliable way to do it.

We don’t need any null values, so how to do it? We will provide an insert store procedure, where we will return the table id, with the help of sqlparameter class, we will store that id value in the first view. In the second view we will not write any insert query instead we will just update the datafield with the help of the same id.

DESIGN CHAMBER

Step 4

Open your data_demo.aspx file to create our design, we will drag a Mutiview Control> Inside that, take two Viewa as Student Details and Student Course.

Data_demo.aspx

ASP.NET

CODE CHAMBER

Step 5

Open data_demo.aspx.cs file to write code for saving the partial data in our database table. 

  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.Data;  
  9. using System.Data.Sql;  
  10. using System.Web.UI.WebControls.WebParts;  
  11. using System.Web.UI.HtmlControls;  
  12.   
  13. public partial class Multiview : System.Web.UI.Page  
  14. {  
  15.     static int id1 = 0;  
  16.     SqlConnection con = new SqlConnection(@"Data Source=Nilesh;Initial Catalog=test_db;Integrated Security=True");  
  17.     protected void Page_Load(object sender, EventArgs e)  
  18.     {  
  19.         if (!IsPostBack)  
  20.         {  
  21.             MultiView1.ActiveViewIndex = 0;  
  22.         }  
  23.   
  24.     }  
  25.   
  26.     protected void Button2_Click(object sender, EventArgs e)  
  27.     {  
  28.         MultiView1.ActiveViewIndex = 1;  
  29.   
  30.           
  31.         SqlCommand cmd = new SqlCommand("sp_insert1", con);  
  32.          
  33.         cmd.CommandType = CommandType.StoredProcedure;  
  34.         SqlParameter outpara = new SqlParameter("@id", SqlDbType.Int);  
  35.         outpara.ParameterName = "@id";  
  36.         outpara.SqlDbType = System.Data.SqlDbType.Int;  
  37.         outpara.Direction = System.Data.ParameterDirection.Output;  
  38.         cmd.Parameters.Add(outpara);        
  39.         cmd.Parameters.AddWithValue("@name", textname.Text);  
  40.         cmd.Parameters.AddWithValue("@city", textcity.Text);  
  41.         con.Open();  
  42.         cmd.ExecuteNonQuery();  
  43.         id1 = Convert.ToInt32(outpara.Value is DBNull ? default(int) : outpara.Value);  
  44.         con.Close();  
  45.         refreshdata();  
  46.   
  47.     }  
  48.   
  49.     public void refreshdata()  
  50.     {    
  51.         SqlCommand cmd = new SqlCommand("select * from tbl_stu", con);  
  52.         SqlDataAdapter sda = new SqlDataAdapter(cmd);  
  53.         DataTable dt = new DataTable();  
  54.         sda.Fill(dt);  
  55.         GridView1.DataSource = dt;  
  56.         GridView1.DataBind();  
  57.   
  58.     }  
  59.   
  60.     protected void Button4_Click(object sender, EventArgs e)  
  61.     {  
  62.         MultiView1.ActiveViewIndex = 2;        
  63.          
  64.     }  
  65.   
  66.     protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)  
  67.     {  
  68.         GridView1.EditIndex = -1;  
  69.         refreshdata();  
  70.   
  71.     }  
  72.   
  73.     protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)  
  74.     {  
  75.        int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["id"].ToString());  
  76.         con.Open();  
  77.         SqlCommand cmd = new SqlCommand("delete from tbl_stu where id =@id", con);  
  78.         cmd.Parameters.AddWithValue("id", id);  
  79.         int i = cmd.ExecuteNonQuery();  
  80.         con.Close();  
  81.         refreshdata();  
  82.   
  83.     }  
  84.   
  85.     protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)  
  86.     {  
  87.         GridView1.EditIndex = e.NewEditIndex;  
  88.         refreshdata();  
  89.   
  90.     }  
  91.   
  92.     protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)  
  93.     {  
  94.         TextBox txtname = GridView1.Rows[e.RowIndex].FindControl("TextBox4") as TextBox;  
  95.         TextBox txtcity = GridView1.Rows[e.RowIndex].FindControl("TextBox5") as TextBox;  
  96.         TextBox txtcourse = GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox;  
  97.         TextBox txtsem = GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox;  
  98.         int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["id"].ToString());  
  99.         con.Open();  
  100.         SqlCommand cmd = new SqlCommand("sp_update", con);  
  101.         cmd.CommandType = CommandType.StoredProcedure;  
  102.         cmd.Parameters.AddWithValue("name", txtname.Text);  
  103.         cmd.Parameters.AddWithValue("city", txtcity.Text);  
  104.         cmd.Parameters.AddWithValue("course", txtcourse.Text);  
  105.         cmd.Parameters.AddWithValue("sem", txtsem.Text);  
  106.         cmd.Parameters.AddWithValue("id", id);  
  107.   
  108.         int i = cmd.ExecuteNonQuery();  
  109.         con.Close();  
  110.         GridView1.EditIndex = -1;  
  111.         refreshdata();  
  112.   
  113.     }  
  114.   
  115.     protected void Button3_Click(object sender, EventArgs e)  
  116.     {  
  117.         MultiView1.ActiveViewIndex = 2;  
  118.         SqlCommand cmd = new SqlCommand("update tbl_stu set course= @course,sem= @sem where id =" + id1.ToString(), con);   
  119.         cmd.Parameters.AddWithValue("@course", TextBox6.Text);  
  120.         cmd.Parameters.AddWithValue("@sem", TextBox7.Text);  
  121.         con.Open();  
  122.         cmd.ExecuteNonQuery();  
  123.         con.Close();  
  124.         refreshdata();  
  125.   
  126.     }  
  127.   
  128.     protected void Button4_Click1(object sender, EventArgs e)  
  129.     {  
  130.         MultiView1.ActiveViewIndex = 0;  
  131.     }  
  132. }   

OUTPUT CHAMBER

Okay! Here, we will insert name and city from the first View and see if it is saving in the database or not.

First View

ASP.NET
Tbl_data - Name and City

ASP.NET

Second View

ASP.NET

Okay, it is working fine,  and saving all the data at one time. Hope you like it. Thank you for reading.

Up Next
    Ebook Download
    View all
    Learn
    View all