Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Monday, 20 July 2015

Introduction:
In this article I will explain how to fill or populate dataset using Stored Procedure  in asp.net. First of we will fetch data from Sql database using Stored Procedure then  fill data in Dataset using SqlDataAdapter.

Implementation: Follow following steps
Create a database i.e. “Test”.  Then create a table “Student_Info”.
Column Name
Datatype
Student_id
Int(Primary Key. So set Is Identity=True)
Student_Name
Varchar(500)
Age
int
Class
Varchar(50)

Now insert some data in this table using “insert” command.
Create Stored Procedure:
create proc Fill_DataSet
as
begin
select * from student_info

end

Create Connection: Now create connection in  webconfig file as given below.
<connectionStrings>
    <add name="con" connectionString="Data Source=localhost; Initial Catalog= Blog; Integrated Security=true;" providerName="System.Data.SqlClient"/>
  </connectionStrings>

ASP.NET code behind File using C#:

Add following Namespaces:
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

C# Code to get data and Fill Data Table:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e) 
    {
        if (!IsPostBack)
        {
            Fill_dataset();
        }
    }
    //Fetch data from database and bind to gridview
public void Fill_dataset()
    {     
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        DataSet ds = new DataSet();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "Fill_Dataset";
        cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter dataadapater = new SqlDataAdapter();
    dataadapater.SelectCommand = cmd;
        dataadapater.Fill(ds);      
    }

VB.NET code:
Add following namespaces:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Now Write Following Code:
Private con As New SqlConnection(ConfigurationManager.ConnectionStrings("con").ConnectionString)
Protected Sub Page_Load(sender As Object, e As EventArgs)
    If Not IsPostBack Then
        Fill_dataset()
    End If
End Sub
'Fetch data from database and fill dataset
Public Sub Fill_dataset()
    If con.State = ConnectionState.Open Then
        con.Close()
    End If
    con.Open()
    Dim ds As New DataSet()
    Dim cmd As New SqlCommand()
    cmd.Connection = con
    cmd.CommandText = "Fill_Dataset"
    cmd.CommandType = CommandType.StoredProcedure
    Dim dataadapater As New SqlDataAdapter()
    dataadapater.SelectCommand = cmd
    dataadapater.Fill(ds)

End Sub

Saturday, 18 July 2015

Introduction:
In this article I will explain how to bind or fill datatable in asp.net using C# and Vb.Net. In this article I have used sql query to fetch data from database and fill the datatable using SQLDataAdapter.


Implementation: Follow following steps
Create a database i.e. “Test”.  Then create a table “Student_Info”.
Column Name
Datatype
Student_id
Int(Primary Key. So set Is Identity=True)
Student_Name
Varchar(500)
Age
int
Class
Varchar(50)

Now insert some data in this table using “insert” command.
Create Connection: Now create connection in  webconfig file as given below.
<connectionStrings>
    <add name="con" connectionString="Data Source=localhost; Initial Catalog= Blog; Integrated Security=true;" providerName="System.Data.SqlClient"/>
  </connectionStrings>

ASP.NET code behind File using C#:

Add following Namespaces:
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

C# Code to get data and Fill Data Table:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e) 
    {
        if (!IsPostBack)
        {
            Bind_datatable();
        }
    }
    //Fetch data from database and bind to gridview
    public void Bind_datatable()
    {     
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        DataTable dt = new DataTable();
        SqlDataAdapter dataadapater = new SqlDataAdapter("Select * from student_info", con);
        dataadapater.Fill(dt);      
    }

VB.NET code:
Add following namespaces:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Now Write Following Code:
Private con As New SqlConnection(ConfigurationManager.ConnectionStrings("con").ConnectionString)
Protected Sub Page_Load(sender As Object, e As EventArgs)
    If Not IsPostBack Then
        Bind_datatable()
    End If
End Sub
'Fetch data from database and bind to gridview
Public Sub Bind_datatable()
    If con.State = ConnectionState.Open Then
        con.Close()
    End If
    con.Open()
    Dim dt As New DataTable()
    Dim dataadapater As New SqlDataAdapter("Select * from student_info", con)
    dataadapater.Fill(dt)

End Sub
In this article I will explain how to disable browser back button after logout in asp.net using JavaScript. Normally when we press back button it will redirected to previous page. So to avoid this we have to disable the browser back button.

Wednesday, 15 July 2015

Introduction:

I have written this article for beginners. In this article I have explained how to insert data from asp.net web page to database. I have used query to insert data to database. 

Tuesday, 14 July 2015

Introduction:
Some time we need to find the position of the first occurrence of a specified value in given string. In this article I will explain how to find position of the first occurrence of a specified value using Javascript.

In this article I have explain how to show Textbox value in Label Control on button click. This article may helpful for beginners.
Implementation:
Add a new webpage in your application. Now Place a textbox control on page. Thena add a button and label control to the page.

Source code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>  
</head>
<body>
    <form id="form1" runat="server">
    <fieldset style="width:400px"><legend><strong>Show Entered value in Label Control</strong></legend>
    <div>
   
        <table >
            <tr>
                <td >
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td >
                    Entyer Value</td>
                <td>
                    <asp:TextBox ID="txtvalue" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td >
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style3">
                </td>
                <td class="style4">
                    <asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click"
                        Text="Submit" />
                </td>
            </tr>
            <tr>
                <td >
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td >
                    Display Entered value</td>
                <td>
                    <asp:Label ID="lblShow" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>
        </table>
   
    </div></fieldset>
    </form>
</body>
</html>


ASP.NET code Using c#:

public partial class insert : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (txtvalue.Text.Trim().Length > 0)
        {
            lblShow.Text = txtvalue.Text;
        }
        else
        {
            lblShow.Text = "Enter value in text box";
        }
    }
}

 VB.NET code:
Partial Class vb
    Inherits System.Web.UI.Page

    Protected Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click
        If txtvalue.Text.Trim().Length > 0 Then
            lblShow.Text = txtvalue.Text
        Else
            lblShow.Text = "Enter value in text box"
        End If
    End Sub
End Class


Use this code in your application and check result.