Multi Database Connectivity Different BackEnd New one

Select the Drop down which BackEnd we want Select then Click Command Button
here more backend list drop down list one application more then backend handled for ex : here we are enter ur values insert this backends msaccess,mssql

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

< asp:ScriptManager ID="ScriptManager1" runat="server" / >

< asp:UpdatePanel ID="Updrp" runat ="server" >
< ContentTemplate >
< asp:DropDownList ID="DrpDblist" runat ="Server" AutoPostBack="True" >
< asp:ListItem Text ="Ms-Access" > < /asp:ListItem >
< asp:ListItem Text ="Sql server" > < /asp:ListItem >
< asp:ListItem Text ="Ms-Excel" > < /asp:ListItem >

< /asp:DropDownList >
< /ContentTemplate >
< /asp:UpdatePanel >


< table width="40%" >
< tr >
< td width="20%" >
Name< /td >
< td width="20%" >
< asp:TextBox ID="txtName" runat="server" > < /asp:TextBox > < /td >
< /tr >
< tr >
< td width="20%" >
Empno< /td >
< td width="20%" >
< asp:TextBox ID="TxtEmpNo" runat="server" > < /asp:TextBox > < /td >
< /tr >
< tr >
< td align="right" colspan="2" >
< asp:Button ID="Bt1" runat="server" Text="ClickHere" / >
< /td >
< /tr >
< tr >
< td align="right" colspan="2" >
< asp:Label ID="lblIntimate" runat="server" ForeColor="#FF0000" Text="" > < /asp:Label > < /td >
< /tr >
< /table >
< asp:UpdatePanel ID="UpdatePanel1" runat="server" >
< ContentTemplate >
< asp:DataGrid ID="DtgGrid" runat ="Server" AllowPaging="True" PageSize="5" >
< PagerStyle Mode="NumericPages" / >
< Columns >
< asp:TemplateColumn HeaderText ="Delete" >
< ItemTemplate >
< asp:ImageButton ID="Delimg" CommandName="Delete" runat ="server" ImageUrl ="~/Images/delete.gif" / >
< /ItemTemplate >
< /asp:TemplateColumn >
< /Columns >
< /asp:DataGrid >
< /ContentTemplate >
< /asp:UpdatePanel >



< /form >
< /body >
< /html >


Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Data.OleDb

Dim Sqlcon As SqlConnection
Dim Sqlcmd As SqlCommand
Dim Sqladp As SqlDataAdapter
Dim Acccon As OleDbConnection
Dim Acccmd As OleDbCommand
Dim Accadp As OleDbDataAdapter
Dim AccDt As Data.DataTable
Dim SqlDt As Data.DataTable
Dim XlDt As Data.DataTable
Dim XlsWb As Object
Dim xlsPath, xlsFile, xlsTab As String



Protected Sub Bt1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Bt1.Click
If DrpDblist.Text = "Ms-Access" Then
Dim AccResult As Boolean = AccessExecute("Insert into tbNamelist values ('" & txtName.Text & "','" & TxtEmpNo.Text & "')")
AccDt = AccCrtb("Select * from tbNamelist", "TbaccTbNAme")
DtgGrid.DataSource = AccDt
DtgGrid.DataBind()
lblIntimate.Text = "Sucessfully save Data Ms-Access Database."
ElseIf DrpDblist.Text = "Sql server" Then
Dim SqlResult As Boolean = SqlExecute("Insert into tbNamelist values ('" & txtName.Text & "','" & TxtEmpNo.Text & "')")
SqlDt = SqlCrtb("Select * from tbNamelist", "TbSqlTbNAme")
DtgGrid.DataSource = SqlDt
DtgGrid.DataBind()
lblIntimate.Text = "Sucessfully save Data to Sql Server Database."
ElseIf DrpDblist.Text = "Ms-Excel" Then
XlsWb = GetObject("D:\AJAXEnabledWebSite2\Data\NameList.xls")
XlsWb.Parent.Windows(1).Visible = True
XlsWb.Application.Worksheets(xlsTab).Cells(0, 1) = txtName.Text
XlsWb.Application.Worksheets(xlsTab).Cells(0, 2) = TxtEmpNo.Text
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
SqlConnect()
SqlDt = SqlCrtb("Select * from tbNamelist", "TbSqlTbNAme")
DtgGrid.DataSource = SqlDt
DtgGrid.DataBind()
End If
If DrpDblist.Text = "Ms-Access" Then
AccessConnect()
ElseIf DrpDblist.Text = "Sql server" Then
SqlConnect()
ElseIf DrpDblist.Text = "Ms-Excel" Then
XlConnection()
End If

End Sub
Public Sub SqlConnect()
Sqlcon = New SqlConnection
Sqlcon.ConnectionString = "Data source=(local);Initial Catalog=TestDb1;Integrated Security=true;"
Sqlcon.Open()
End Sub
Public Sub AccessConnect()
Acccon = New OleDbConnection
Acccon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\AJAXEnabledWebSite2\Data\NameList.mdb"
Acccon.Open()
End Sub
Public Sub XlConnection()
Acccon = New OleDbConnection
Acccon.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\AJAXEnabledWebSite2\Data\NameList.xls;Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
Acccon.Open()
End Sub
Public Function SqlExecute(ByVal sqry As String) As Boolean
Dim Sqlcmd As New SqlCommand(sqry, Sqlcon)
Sqlcmd.ExecuteNonQuery()
End Function
Public Function AccessExecute(ByVal sqry As String) As Boolean
Dim Acccmd As New OleDbCommand(sqry, Acccon)
Acccmd.ExecuteNonQuery()
End Function
Public Function SqlCrtb(ByVal str12 As String, ByVal sttb As String) As Data.DataTable
Dim dt As New Data.DataTable
Dim Sqladp As New SqlDataAdapter()
Dim Sqlcmd As New SqlCommand(str12, Sqlcon)
Sqladp.SelectCommand = Sqlcmd
dt.TableName = sttb
Sqladp.Fill(dt)
SqlCrtb = dt
End Function
Public Function AccCrtb(ByVal str1 As String, ByVal sttb As String) As Data.DataTable
Dim dt As New Data.DataTable
Dim Accadp As New OleDbDataAdapter()
Dim Acccmd As New OleDbCommand(str1, Acccon)
Accadp.SelectCommand = Acccmd
dt.TableName = sttb
Accadp.Fill(dt)
AccCrtb = dt
End Function
Protected Sub DtgGrid_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DtgGrid.DeleteCommand
If DrpDblist.Text = "Ms-Access" Then
AccDt = AccCrtb("Select * from tbNamelist", "TbaccTbNAme")
AccDt.Rows(e.Item.ItemIndex).Delete()
DtgGrid.DataSource = AccDt
DtgGrid.DataBind()
End If

If DrpDblist.Text = "Sql server" Then
SqlDt = SqlCrtb("Select * from tbNamelist", "TbSqlTbNAme")
SqlDt.Rows(e.Item.ItemIndex).Delete()
DtgGrid.DataSource = SqlDt
DtgGrid.DataBind()
End If
End Sub
Protected Sub DtgGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DtgGrid.PageIndexChanged
DtgGrid.CurrentPageIndex = e.NewPageIndex
If DrpDblist.Text = "Ms-Access" Then
AccDt = AccCrtb("Select * from tbNamelist", "TbaccTbNAme")
DtgGrid.DataSource = AccDt
DtgGrid.DataBind()
ElseIf DrpDblist.Text = "Sql server" Then
SqlDt = SqlCrtb("Select * from tbNamelist", "TbSqlTbNAme")
DtgGrid.DataSource = SqlDt
DtgGrid.DataBind()
End If
End Sub


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: