Saturday, March 14, 2009

Database To XML Conversion Tool

<!-- This programs needs ASP.NET 2.0 and SQL Server -->

<%@ Page Language="VB" %>
<%@ Import Namespace="System.data.sqlclient" %>
<%@ Import Namespace="System.data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' check any filename is entered
If TextBox1.Text.Trim.Length = 0 Then
Label1.Text = "Enter file name in the textbox."
Return
End If
' check whether any column is selected
If CheckBoxList1.SelectedIndex = -1 Then
Label1.Text = "Select Columns!"
Return
End If
' get columns
Dim item As ListItem
Dim columns As String = ""
Dim tname As String = DropDownList1.SelectedItem.Text
For Each item In CheckBoxList1.Items
If item.Selected Then
columns &= "," & item.Text
End If
Next
columns = columns.Substring(1)
Dim con As New SqlConnection("uid=sa;database=pubs")
Dim da As New SqlDataAdapter("select " & columns & " from " + tname, con)
Dim ds As New DataSet
da.Fill(ds, tname)
ds.WriteXml(Request.MapPath(TextBox1.Text))
Label1.Text = "Created XML File : " & TextBox1.Text
End Sub

Protected Sub btnSelectAll_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim item As ListItem
For Each item In CheckBoxList1.Items
item.Selected = True
Next
End Sub


Protected Sub btnUnselect_Click(ByVal sender As Object, ByVal e As System.EventArgs)
CheckBoxList1.ClearSelection()
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body bgcolor="lightgrey">
<form id="form1" runat="server">

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
SelectCommand="select id,name from sysobjects where xtype='u'"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
SelectCommand="select name from syscolumns where id = @id">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="id" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<h2>
<span style="color: #000033">Database To XML Conversion Tool </span></h2>
<p></p>
<table>
<tr>
<td bgcolor="whitesmoke">
Select Table :
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1"
DataTextField="name" DataValueField="id" Width="135px">
</asp:DropDownList>
<p />
Select Columns
<br />
<asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSourceID="SqlDataSource2"
DataTextField="name" DataValueField="name">
</asp:CheckBoxList><br />
<asp:Button ID="btnSelectAll" runat="server" OnClick="btnSelectAll_Click" Text="Select All" />
<asp:Button ID="btnUnselect" runat="server" Text="Unselect All" OnClick="btnUnselect_Click" /><br />
 

</td>
<td>
Enter Target File <br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Create XML" />
<br />
<br />
<asp:Label ID="Label1" runat="server" EnableViewState="False" Font-Bold="True" Font-Size="Medium" ForeColor="Blue"></asp:Label></td>
</tr>
</table>
<br />
</form>
</body>
</html>

Labels:

How to Display content of text file using Datagrid

<%@ page debug=true%>
<%@ import namespace="system.data,system.io"%>


<script runat=server>

sub page_load()
dim dt as new DataTable()

dim c_name as new DataColumn("Name", Type.GetType("System.String"))
dim c_email as new DataColumn("Email", Type.GetType("System.String"))

dt.columns.add(c_name)
dt.columns.add(c_email)


' read data from file in the current directory
' file contains emails in the format name:email

dim sr as new StreamReader( server.mappath("emails.txt"))

dim line as string
dim parts(2) as string
dim dr as datarow

line = sr.Readline()

' read lines until you reach EOF

do until line is nothing

' split the line into two - name and email

parts = line.split(":")

if parts.length = 2 then

' add a new row to datatable
dr = dt.NewRow()
dr.item(0) = parts(0)
dr.item(1) = parts(1)

dt.rows.add(dr)
end if

' read next line

line = sr.ReadLine()

loop

sr.close()

dg1.datasource = dt
dg1.DataBind()


end sub

</script>

<form runat=server>
<h2>Email Addresses </h2>
<asp:datagrid id=dg1 runat=server />
</form>

Labels:

How to Deleting Rows From DataGrid

<!-- The following form displays rows in datagrid along with a checkbox in each row.
Rows can be deleted by turning the checkbox on and then clicking on Delte button --!>


<%@ Page Language="VB" %>
<%@ import Namespace="System.Data,system.Data.SqlClient" %>

<script runat="server">

Dim ConnectionString As String = "uid=sa;database=pubs"

Sub Page_Load(Sender As Object, E As EventArgs)

If Not Page.IsPostBack Then
BindData()
End If

End Sub

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

Dim i As Integer = 0
Dim cb As CheckBox
Dim dgi As DataGridItem
Dim id As Integer

dim con as new SQlConnection( connectionstring)
con.open()

dim cmd as new SQLCommand("delete from sometable where id = @id",con)
dim p as new SQLParameter("@id",sqldbtype.int)
cmd.parameters.add(p)


For Each dgi In DataGrid1.Items
cb = CType(dgi.findcontrol("chkdelete"), CheckBox)

If cb.Checked Then
id = CType( DataGrid1.DataKeys(i), Integer) ' get primary key
p.value = uid
try
cmd.ExecuteNonQuery()
catch ex as Exception
response.write( ex.message)
end try

End If
i += 1
Next

con.close()

binddata()

End Sub

Sub BindData()

Dim myConnection As New SqlConnection(ConnectionString)
Dim myCommand As New SqlDataAdapter("select * from sometable", myConnection)

Dim ds As New DataSet()
myCommand.Fill(ds)

DataGrid1.DataSource = ds
DataGrid1.DataBind()

End Sub

</script>

<html>
<body>

<form runat="server">
<asp:datagrid id="DataGrid1" runat="server" DataKeyField="id">
<Columns>
<asp:templateColumn>
<headertemplate>Delete</headertemplate>
<itemtemplate>
<asp:checkbox id=chkdelete runat="server"></asp:checkbox>
</itemtemplate>
</asp:templateColumn>
</Columns>
</asp:datagrid>
<p/>
<asp:button id="btndelete" runat="server" text="Delete" onclick="btndelete_click"/>
</form>
</body>
</html>

Labels:

How to Displaying serial number in DataGrid

<%@ Import="" namespace="System.Data" %>
<%@ Import="" namespace="System.Data.SQLClient" %>

<script language="VB" runat="server">

Sub Page_Load (sender as object, e as eventargs)

Dim con as new SQLConnection("uid=sa;database=pubs")
con.open()
dim cmd as new SQLCommand("select title,type, price from titles",con)
dim dr as Sqldatareader
dr = cmd.ExecuteReader()
datagrid1.datasource = dr
datagrid1.databind()
dr.close()
con.close()

End Sub

Sub populateSerialNumber(sender as object, objargs as DataGridItemEventArgs)
If objArgs.Item.ItemType <> ListItemType.Header Then
objArgs.Item.Cells(0).text = objArgs.Item.datasetindex + 1
End If
End sub
</script>


<form Runat="server">
<Asp:DataGrid Runat="server" id="datagrid1" OnItemDataBound = "PopulateSerialNumber">
<Columns>
<Asp:TemplateColumn HeaderText="S.No" ItemStyle-HorizontalAlign="right">
<ItemTemplate></ItemTemplate>
</Asp:TemplateColumn>
</columns>

</Asp:Datagrid>
</form>

Labels: