Database connection not open properly

abhit_kumar

Member
Joined
Aug 13, 2008
Messages
19
Programming Experience
Beginner
hello all,

i have devlope the windows apllication and my database in MS Access.
In my Application some Button on form such as Save,Update,Delete,FileUpload....etc,
and connection string get in the .config file.
the code is below:
<appSettings>
<add key="ConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\DataBase\ECS.mdb"/>
</appSettings>

i have make and use the object of open the connection .

But in My Application when i have click the save button then Oledb connection open and
data save in table properly.
after then i have upload my .txt file and save the data so oledb connection is not open.
and throw the error.

plz, give the soluation

My code is below:
VB.NET:
1    Imports System.Data.OleDb
2    Imports System.Data
3    
4    Public Class clsHomePage
5    
6        Dim oledbCon As OleDbConnection
7        Dim oledbAd As OleDbDataAdapter
8        Dim oledCom As OleDbCommand
9        Dim ds As DataSet
10       Dim dr As DataRow
11       Dim pos As Integer = 0
12       Dim strFileId As Integer
13       'Dim txtRBIFile_ As String = "RBIFile_"
14   
15       Public Function fun_GetConnection() As OleDbConnection
16           Try
17               Dim str_Connection As String = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=" & "..\DataBase\ECS.mdb"
18               'Dim str_Connection As String = System.Configuration.ConfigurationSettings.AppSettings("ConnectionString")
19               oledbCon = New OleDbConnection(str_Connection)
20               oledbCon.Open()
21           Catch ex As Exception
22               Throw ex
23           End Try
24           Return oledbCon
25       End Function
26   
27       Public Function fun_Insert_AccountData(ByVal AccountNo As String, ByVal Amount As Integer, ByVal Cust_Name As String, ByVal Comp_Name As String, ByVal Date_Payment As Date, ByVal Remark1 As String, ByVal Remark2 As String, ByVal LinesNo As String) As Boolean
28           Try
29               oledCom = New OleDbCommand("Insert Into Account_Master (AccountNo,Amount,Cust_Name,Comp_Name,Date_Payment,Remark1,Remark2,LinesNo) Values('" & AccountNo & "'," & Amount & ",'" & Cust_Name & "','" & Comp_Name & "','" & Date_Payment & "','" & Remark1 & "','" & Remark2 & "','" & LinesNo & "')", fun_GetConnection())
30               oledCom.ExecuteNonQuery()
31               Return True
32           Catch ex As Exception
33               Throw ex
34               Return False
35           Finally
36               fun_GetConnection().Dispose()
37           End Try
38       End Function
39   
40       Public Function fun_Insert_UploadFile(ByVal Orignal_FileName As String)
41           Try
42               oledbAd = New OleDbDataAdapter("Select max(File_Id)as FileId From FileInfo_Master", fun_GetConnection())
43               ds = New DataSet
44               oledbAd.Fill(ds)
45               If ds.Tables("FileInfo_Master").Rows.Count > 0 Then
46                   dr = ds.Tables("FileInfo_Master").Rows(pos)
47                   strFileId = IIf((IsDBNull(dr(0)) = True), 0, dr(0)) + 1
48               End If
49               'txtRBIFile_ = txtRBIFile_ + strFileId
50               oledCom = New OleDbCommand("Insert Into FileInfo_Master (File_Id,Orignal_FileName,Assign_FileName,Upload_Date) Values(" & strFileId & ",'" & Orignal_FileName & "','" & 1 & "','" & Now.Date & "')", fun_GetConnection())
51               oledCom.ExecuteNonQuery()
52               Return True
53           Catch ex As Exception
54               Throw ex
55               Return False
56           Finally
57               fun_GetConnection().Close()
58           End Try
59       End Function
60   
61       Public Function fun_Get_AccountInfo() As DataSet
62           Try
63               oledbAd = New OleDbDataAdapter("Select AccountNo,Amount,Cust_Name as CustomerName,Comp_Name as BankName,Date_Payment as PaymentDate,Remark1,Remark2,LinesNo  from Account_Master order by Date_Payment", fun_GetConnection())
64               ds = New DataSet("Account_Master")
65               oledbAd.Fill(ds, "Account_Master")
66           Catch ex As Exception
67               Throw ex
68           Finally
69               fun_GetConnection().Dispose()
70           End Try
71           Return ds
72       End Function
73   
74       Public Function fun_Get_AccountInfo(ByVal strAccountNo As String) As DataSet
75           Try
76               ds = New DataSet
77               'oledbAd = New OleDbDataAdapter("Select MID(AccountNo,Len(AccountNo)-5,6),Amount,Cust_Name,Comp_Name,Date_Payment,Remark1,Remark2,LinesNo from Account_Master where " & strAccountNo.ToString, fun_GetConnection())
78               'oledbAd = New OleDbDataAdapter("Select AccountNo,mid(AccountNo,Len(AccountNo)-5,6),Amount,Cust_Name,Comp_Name,Date_Payment,Remark1,Remark2,LinesNo from Account_Master where " & strAccountNo.ToString.Trim & "Group by AccountNo,Amount,Cust_Name,Comp_Name,Date_Payment,Remark1,Remark2,LinesNo", fun_GetConnection())
79               oledbAd = New OleDbDataAdapter("Select AccountNo,Amount,Cust_Name as CustomerName,Comp_Name as BankName,Date_Payment as PaymentDate,Remark1,Remark2,LinesNo  from Account_Master where " & strAccountNo.ToString, fun_GetConnection())
80               oledbAd.Fill(ds, "Account_Master")
81           Catch ex As Exception
82               Throw ex
83           End Try
84           Return ds
85       End Function
86   
87       Public Function open_connection(ByVal abc As String)
88           oledbCon = New OleDbConnection()
89           oledbCon = fun_GetConnection()
90           Return True
91       End Function
92   End Class
93


thanks
Anil kapadia
 
For a guide on how to do your data access properly, see the DW2 link in my signature, section "Creating a Simple Data App"
 
Back
Top