Results 1 to 13 of 13

Thread: Data base connectivity problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Location
    michigan
    Posts
    70

    Data base connectivity problem

    If there are two forms 1) Main form 2) Sub Form

    In the Main Form there is a button which leads to the sub Form
    The Sub form has got some database connectivity in that..
    Do i have to create the connection in the main form or is it fine if I create it in the sub Form cuz Im getting security exceptions.

    Can someone please help me out??? I appreicate ur help in advance

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Location
    michigan
    Posts
    70

    Actually Problem with the data base connectivity

    Actually Im having a problem with the database connectivity it says:

    An unhandled exception of type 'System.Security.SecurityException' occurred in Sample.exe

    Can any1 help me out???

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Are you connection to local or remote database ? Is it password-protected ?

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    btw , it's gonna be more convenient to declare the connection obj in a module so it can be accessed from anywhere in your proj .

  5. #5
    Addicted Member
    Join Date
    Mar 2001
    Location
    Devon, UK
    Posts
    181
    Create a seperate database component which can be called from anywhere in the program.
    Pass your stored proc and parameters in to return a datareader object or a dataset object (or a collection - what ever you want!).
    Wind and waves resolves all problems.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Location
    michigan
    Posts
    70
    I am using MS-Access and the database is not remote itz local database.

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You may want to post some code for more help.

  8. #8
    Lively Member
    Join Date
    Sep 2003
    Location
    Chicago, IL
    Posts
    64
    You should create a class that wraps all data access to that database (or any database). that way as far as your project code goes you are working with an object and not data access code directly. this will make your project much easier to modify and maintain as well.


    basic connection code (this is for an Informix database):

    declared at class level

    Private _cnWMS As OleDb.OleDbConnection

    in public sub new() or wherever you want

    try
    _cnWMS = New OleDb.OleDbConnection("Provider=Ifxoledbc.2;Password=pw;Persist Security Info=True;User ID=userID;Data Source=viaware@conwaywdsoc")

    _cnWMS.open

    <blah blah data access happens here>
    catch ex as exception
    throw new exception ("Error in whatever your function or sub was",ex)
    finally
    _cnWMS.close
    end try
    Last edited by MikeStammer; Sep 30th, 2003 at 10:58 AM.
    Mike Stammer

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Location
    michigan
    Posts
    70

    herez the code

    VB Code:
    1. Public Class Form1
    2.     Inherits System.Windows.Forms.Form
    3.  
    4. #Region " Windows Form Designer generated code "
    5.  
    6.     Public Sub New()
    7.         MyBase.New()
    8.  
    9.         'This call is required by the Windows Form Designer.
    10.         InitializeComponent()
    11.  
    12.         'Add any initialization after the InitializeComponent() call
    13.  
    14.     End Sub
    15.  
    16.     'Form overrides dispose to clean up the component list.
    17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    18.         If disposing Then
    19.             If Not (components Is Nothing) Then
    20.                 components.Dispose()
    21.             End If
    22.         End If
    23.         MyBase.Dispose(disposing)
    24.     End Sub
    25.  
    26.     'Required by the Windows Form Designer
    27.     Private components As System.ComponentModel.IContainer
    28.  
    29.     'NOTE: The following procedure is required by the Windows Form Designer
    30.     'It can be modified using the Windows Form Designer.  
    31.     'Do not modify it using the code editor.
    32.     Friend WithEvents lblFirstName As System.Windows.Forms.Label
    33.     Friend WithEvents lblLastName As System.Windows.Forms.Label
    34.     Friend WithEvents lblAddress As System.Windows.Forms.Label
    35.     Friend WithEvents txtFirstName As System.Windows.Forms.TextBox
    36.     Friend WithEvents txtLastName As System.Windows.Forms.TextBox
    37.     Friend WithEvents txtAddress As System.Windows.Forms.TextBox
    38.     Friend WithEvents btnSubmit As System.Windows.Forms.Button
    39.     Friend WithEvents odbconSam As System.Data.OleDb.OleDbConnection
    40.     Friend WithEvents odbdaSample As System.Data.OleDb.OleDbDataAdapter
    41.     Friend WithEvents DsSample1 As Sample.dsSample
    42.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    43.         Me.lblFirstName = New System.Windows.Forms.Label()
    44.         Me.lblLastName = New System.Windows.Forms.Label()
    45.         Me.lblAddress = New System.Windows.Forms.Label()
    46.         Me.txtFirstName = New System.Windows.Forms.TextBox()
    47.         Me.txtLastName = New System.Windows.Forms.TextBox()
    48.         Me.txtAddress = New System.Windows.Forms.TextBox()
    49.         Me.btnSubmit = New System.Windows.Forms.Button()
    50.         Me.odbconSam = New System.Data.OleDb.OleDbConnection()
    51.         Me.odbdaSample = New System.Data.OleDb.OleDbDataAdapter()
    52.         Me.DsSample1 = New Sample.dsSample()
    53.         CType(Me.DsSample1, System.ComponentModel.ISupportInitialize).BeginInit()
    54.         Me.SuspendLayout()
    55.         '
    56.         'lblFirstName
    57.         '
    58.         Me.lblFirstName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
    59.         Me.lblFirstName.Location = New System.Drawing.Point(72, 32)
    60.         Me.lblFirstName.Name = "lblFirstName"
    61.         Me.lblFirstName.Size = New System.Drawing.Size(104, 32)
    62.         Me.lblFirstName.TabIndex = 0
    63.         Me.lblFirstName.Text = "FirstName"
    64.         '
    65.         'lblLastName
    66.         '
    67.         Me.lblLastName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
    68.         Me.lblLastName.Location = New System.Drawing.Point(72, 80)
    69.         Me.lblLastName.Name = "lblLastName"
    70.         Me.lblLastName.Size = New System.Drawing.Size(104, 32)
    71.         Me.lblLastName.TabIndex = 1
    72.         Me.lblLastName.Text = "LastName"
    73.         '
    74.         'lblAddress
    75.         '
    76.         Me.lblAddress.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
    77.         Me.lblAddress.Location = New System.Drawing.Point(72, 128)
    78.         Me.lblAddress.Name = "lblAddress"
    79.         Me.lblAddress.Size = New System.Drawing.Size(104, 32)
    80.         Me.lblAddress.TabIndex = 2
    81.         Me.lblAddress.Text = "Address:"
    82.         '
    83.         'txtFirstName
    84.         '
    85.         Me.txtFirstName.Location = New System.Drawing.Point(200, 32)
    86.         Me.txtFirstName.Multiline = True
    87.         Me.txtFirstName.Name = "txtFirstName"
    88.         Me.txtFirstName.Size = New System.Drawing.Size(112, 32)
    89.         Me.txtFirstName.TabIndex = 3
    90.         Me.txtFirstName.Text = ""
    91.         '
    92.         'txtLastName
    93.         '
    94.         Me.txtLastName.Location = New System.Drawing.Point(200, 80)
    95.         Me.txtLastName.Multiline = True
    96.         Me.txtLastName.Name = "txtLastName"
    97.         Me.txtLastName.Size = New System.Drawing.Size(112, 32)
    98.         Me.txtLastName.TabIndex = 4
    99.         Me.txtLastName.Text = ""
    100.         '
    101.         'txtAddress
    102.         '
    103.         Me.txtAddress.Location = New System.Drawing.Point(200, 128)
    104.         Me.txtAddress.Multiline = True
    105.         Me.txtAddress.Name = "txtAddress"
    106.         Me.txtAddress.Size = New System.Drawing.Size(112, 32)
    107.         Me.txtAddress.TabIndex = 5
    108.         Me.txtAddress.Text = ""
    109.         '
    110.         'btnSubmit
    111.         '
    112.         Me.btnSubmit.Location = New System.Drawing.Point(72, 200)
    113.         Me.btnSubmit.Name = "btnSubmit"
    114.         Me.btnSubmit.Size = New System.Drawing.Size(112, 40)
    115.         Me.btnSubmit.TabIndex = 6
    116.         Me.btnSubmit.Text = "Submit"
    117.         '
    118.         'odbconSam
    119.         '
    120.         Me.odbconSam.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=Z:\course\" & _
    121.         "Chapter.04\Data\sample.mdb;Mode=Share Deny None;Extended Properties="""";Jet OLEDB" & _
    122.         ":System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Je" & _
    123.         "t OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial" & _
    124.         " Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password" & _
    125.         "="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet " & _
    126.         "OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repai" & _
    127.         "r=False;Jet OLEDB:SFP=False"
    128.         '
    129.         'odbdaSample
    130.         '
    131.         Me.odbdaSample.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "tblSample", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("fldAddress", "fldAddress"), New System.Data.Common.DataColumnMapping("fldFirstName", "fldFirstName"), New System.Data.Common.DataColumnMapping("fldLastName", "fldLastName")})})
    132.         '
    133.         'DsSample1
    134.         '
    135.         Me.DsSample1.DataSetName = "dsSample"
    136.         Me.DsSample1.Locale = New System.Globalization.CultureInfo("en-US")
    137.         Me.DsSample1.Namespace = "http://www.tempuri.org/dsSample.xsd"
    138.         '
    139.         'Form1
    140.         '
    141.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    142.         Me.ClientSize = New System.Drawing.Size(584, 469)
    143.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnSubmit, Me.txtAddress, Me.txtLastName, Me.txtFirstName, Me.lblAddress, Me.lblLastName, Me.lblFirstName})
    144.         Me.Name = "Form1"
    145.         Me.Text = "Form1"
    146.         CType(Me.DsSample1, System.ComponentModel.ISupportInitialize).EndInit()
    147.         Me.ResumeLayout(False)
    148.  
    149.     End Sub
    150.  
    151. #End Region
    152.  
    153.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    154.         Dim pbndTemp As Binding, pintRecords As Integer
    155.         pintRecords = odbdaSample.Fill(DsSample1)
    156.  
    157.         pbndTemp = New Binding("Text", DsSample1, "tblSample.fldFirstName")
    158.         txtFirstName.DataBindings.Add(pbndTemp)
    159.  
    160.         pbndTemp = New Binding("Text", DsSample1, "tblSample.fldLastName")
    161.         txtLastName.DataBindings.Add(pbndTemp)
    162.  
    163.         pbndTemp = New Binding("Text", DsSample1, "tblSample.Address")
    164.         txtAddress.DataBindings.Add(pbndTemp)
    165.     End Sub
    166. End Class
    Last edited by cooldudeattitud; Sep 30th, 2003 at 01:29 PM.

  10. #10
    Lively Member
    Join Date
    Sep 2003
    Location
    Chicago, IL
    Posts
    64
    First, I would lose the data object controls and work with them in pure code.

    second, what line is your error being thrown?

    third, databinding like that is evil. Pure evil.

    Finally, always always always always use a try catch block around all code. this way you can get the stacktraces and error messages when something bad happens.

    What are you trying to accomplish? If I could get a copy of that database and what the requirements are I could put something together for you. If you are interested, send me a private message and we can arrange it.
    Mike Stammer

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Location
    michigan
    Posts
    70
    It gives error at INITIALIZECOMPONENT() saying that system.security.securityexception error occured

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Location
    michigan
    Posts
    70
    Thanx a lot mike it did work I appreciate ur help man

  13. #13
    Lively Member
    Join Date
    Sep 2003
    Location
    Chicago, IL
    Posts
    64
    turns out his database was on a network share. This is why he was getting a security exception. Either set Intranet to full trust of move the database to the local machine and its all good.
    Mike Stammer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width