locked
How to code if database1 is nothing use database2 RRS feed

  • Question

  • User-775831949 posted

    I have an accessdatasource1 to feed data to Formview and I want if this database1 is nil (ie no record) I want to use accessdatasource2 to feed data instead.

    How to do this ? Thanks 

    Sunday, November 14, 2010 11:19 PM

Answers

  • User1619573650 posted

    Imports System.IO
    Imports System.Data
    Imports System.Data.OleDb
    Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'get data to your data set
      Dim myCon As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=youraccessdatasource1path;User Id=;Password=;")
    Dim myCmd As New OleDbCommand("select * from table1", myCon)
    myCmd.CommandType = CommandType.Text
    Dim myDa As New OleDbDataAdapter()
    myCon.Open()
    myDa.SelectCommand = myCmd
    Dim myDs As New DataSet()
    myDa.Fill(myDs)
    myCmd.Dispose()
    myDa.Dispose()
    myCon.Close()
    If (myDs.Tables(0).Rows.Count > 0) Then
    'bind to formview to datasource
    Else
    'get data from datasource2
    End If
    End Sub
    End Class 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 15, 2010 3:15 AM

All replies

  • User1619573650 posted

    From what i understood, do mean to say the table data in database 1 is empty !  If this is the case, you might probably need a conditional Logic in your code behind to see if your table in database 1 is returning any data.

        E.g if you are using a dataset object to bind your form view do a count on the dataset to find if it has any data?

    if there is no data returned then open a new connection to database 2 and populate your data source object.

    Monday, November 15, 2010 1:02 AM
  • User-775831949 posted

    Yes the code behind code on condition to see if datasource1 is empty is exactly what I need.

    Something like:

    If Accessdatasource1 is nothing then XXX

    But I tried it did not work. Pls help. Thanks

    Monday, November 15, 2010 1:05 AM
  • User1619573650 posted

    could you please post your code? 

    Monday, November 15, 2010 1:16 AM
  • User-775831949 posted

    Like this, thanks.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            If AccessDataSource1 Is Nothing Then
                Label7.Text = "Ok"
            End If
        End Sub

    Now label7 does not return "Ok" 

    Monday, November 15, 2010 1:21 AM
  • User1619573650 posted

    This checks if the database exists. and once it is done you have to write code to retrieve data from the corresponding database. Hope it helps...


    Imports System.IO
    Partial Class _Default
      Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If File.Exists("youraccessDatabase.accdb") Then ' specefy the absolute path for your access database and .accdb is the extension for access database
      Label7.Text = "datasource1:"
    'do operations from datasource 1
    Else
      Label7.Text = "datasoruce2"
    'do operaions from database 2
    End If
    End Sub
    End Class

    Monday, November 15, 2010 1:51 AM
  • User-775831949 posted

    Thanks a lot. The condition actually is not whether the file exist or not. The mdb file is always there, it is just that in some case, Table1 contains no record. How should I write the if statement ? This is my code for the datasource now

    <asp:AccessDataSource ID="AccessDataSource1" runat="server"
            DataFile="~/contact.mdb" SelectCommand="SELECT * FROM [Table1]"></asp:AccessDataSource>

    the file contact.mdb always exists.

     Thanks

     

     

    Monday, November 15, 2010 2:07 AM
  • User1619573650 posted

    fine.. all that you have to do is

    1. Open a connection with accessdtasource1

    2. get the data  from table1 in accessdatasource1 to a dataset

    3. do a dataset.tabels[0].rows.count if you have data its fine else go for datasource2 

    Monday, November 15, 2010 2:12 AM
  • User-775831949 posted

    Thanks. i will do the code for 1,3.

    But I dunno how to write 2. Could you pls help. Sorry.

     

    Monday, November 15, 2010 2:41 AM
  • User1619573650 posted

    Imports System.IO
    Imports System.Data
    Imports System.Data.OleDb
    Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'get data to your data set
      Dim myCon As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=youraccessdatasource1path;User Id=;Password=;")
    Dim myCmd As New OleDbCommand("select * from table1", myCon)
    myCmd.CommandType = CommandType.Text
    Dim myDa As New OleDbDataAdapter()
    myCon.Open()
    myDa.SelectCommand = myCmd
    Dim myDs As New DataSet()
    myDa.Fill(myDs)
    myCmd.Dispose()
    myDa.Dispose()
    myCon.Close()
    If (myDs.Tables(0).Rows.Count > 0) Then
    'bind to formview to datasource
    Else
    'get data from datasource2
    End If
    End Sub
    End Class 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 15, 2010 3:15 AM