A sample to help in other samples related DataSource
There are quite a few times, when I get an issue related with a specific DataControl and I want to bind it to a datasource. All geared up and with full enthusiasm, I start dealing with the issue... and DARN... I find that I did something awfully bad with my SQL Server during my last case!! So now, instead of fixing my core issue, I end up fixing my SQL Server. Since we don't really like digressing from the main issue I am posting this entry on my blog so that I could keep it handy and instead of fixing the SQL server, I can concentrate on fixing the root cause of the issue. By the way, SQL server rocks... more often than not, it is my bad that I end up doing something silly against it
SamplePage1.aspx in C#
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public class SampleDataSource
{
DataTable dt;
public SampleDataSource()
{
dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Description", typeof(string));
dt.Columns.Add("Address", typeof(string));
for (int i = 0; i < 10; i++)
{
DataRow dr;
dr = dt.NewRow();
dr["ID"] = i;
dr["Name"] = "Some Name " + i.ToString();
dr["Description"] = "Some Description " + i.ToString();
dr["Address"] = "Some Address " + i.ToString();
dt.Rows.Add(dr);
}
}
public DataTable DataSource
{
get
{
return this.dt;
}
}
}
protected void Populate_Click(object sender, EventArgs e)
{
SampleDataSource s = new SampleDataSource();
GridView1.DataSource = s.DataSource;
GridView1.DataBind();
}
</script>
<html xmlns="https://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" Width="50%" runat="server">
</asp:GridView>
<asp:Button ID="Populate" runat="server" Text="Populate" onclick="Populate_Click" />
</div>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Public Class SampleDataSource
Dim dt As DataTable
Public Sub New()
Dim i As Integer
dt = New DataTable
dt.Columns.Add("ID", GetType(Integer))
dt.Columns.Add("Name", GetType(String))
dt.Columns.Add("Description", GetType(String))
dt.Columns.Add("Address", GetType(String))
For i = 0 To 10
Dim dr As DataRow
dr = dt.NewRow
dr("ID") = i
dr("Name") = "Some Name " + i.ToString()
dr("Description") = "Some Description " + i.ToString()
dr("Address") = "Some Address " + i.ToString()
dt.Rows.Add(dr)
Next
End Sub
Public ReadOnly Property DS() As DataTable
Get
Return dt
End Get
End Property
End Class
Protected Sub btnPopulate_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim sd As New SampleDataSource()
dgSample.DataSource = sd.DS
dgSample.DataBind()
End Sub
</script>
<html xmlns="https://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="dgSample" width="50%" runat="server"></asp:GridView>
<asp:Button ID="btnPopulate" Text="Populate" runat="server" onclick="btnPopulate_Click" />
</div>
</form>
</body>
</html>
I know this might not be helpful to most of you at all since this code doesn't do much by itself. I have certain samples to do, and may be in future, this post will help me and some of my folks here while troubleshooting!
Until next time
Rahul
Quote of the day:
Every hero becomes a bore at last. - Ralph Waldo Emerson
Comments
- Anonymous
May 15, 2008
PingBack from http://kaden.freemedianewstoday.info/vbsubmainform.html