Thursday, 2 January 2014

how to get data from database in datatable / how to get values in datatable

Posted on 05:36 in
Introduction:
This article is helps to how to get values from database in datatable formats using asp.net in code behind. Here I explain some data should be insert into a particular table. After we get a data from database datatable formats using dataadapter methods.  
VB.NET:
Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs
Handles Me.Load
        Dim con As NewSqlConnection("server=192.168.1.100;uid=sa;pwd=sa123;
database=asp_dotnet")
        con.Open()
        Dim cmd As New SqlCommand("SELECT * FROM STUDENTDETAILS", con)
        Dim da As New SqlDataAdapter(cmd)
        Dim dt As New DataTable()
        da.Fill(dt)
        con.Close()
End Sub
C#.NET:
  protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con = newSqlConnection("server=192.168.1.100;uid=sa;pwd=sa123;database=asp_dotnet");
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM STUDENTDETAILS", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            con.Close();
        }

Demo:

0 comments:

Post a Comment