|
-
Apr 15th, 2010, 06:04 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] Data isnt getting Saved, using oledb cmd
well this is the code i have to save the data to the db, but it isnt giving any error nor the data is been saved
vb Code:
Private Conn As OleDb.OleDbConnection
Private cmd As OleDb.OleDbCommand
Private sql As String
Private Sub Members_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Conn = New OleDb.OleDbConnection(My.Settings.DMSConnectionString)
cmd = New OleDb.OleDbCommand(sql, Conn)
Conn.Open()
Catch ex As Exception
MessageBox.Show("Error Connecting to Database", "DMS", MessageBoxButtons.OK)
Me.Close()
End Try
End Sub
Private Sub CmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdSave.Click
sql = "INSERT INTO Members ( MemberName, FatherName, Age, Gender, BloodGroup, WorkSection, Occupation, PhotoLink ) "
sql &= "Values (@MemberName, @FatherName, @Age, @Gender, @BloodGroup, @WorkSection, @Occupation, @PhotoLink ) "
cmd.CommandText = sql
cmd.Parameters.AddWithValue("@MemberName", Me.MemberName.Text.Trim)
cmd.Parameters.AddWithValue("@FatherName", Me.FatherName.Text.Trim)
cmd.Parameters.AddWithValue("@Age", Me.Age.Text.Trim)
cmd.Parameters.AddWithValue("@Gender", Me.Gender.Text)
cmd.Parameters.AddWithValue("@BloogGroup", Me.BloodGroup.Text)
cmd.Parameters.AddWithValue("@WorkSection", Me.WorkSection.Text.Trim)
cmd.Parameters.AddWithValue("@Occupation", Me.Occupation.Text.Trim)
cmd.Parameters.AddWithValue("@PhotoLink", "")
cmd.ExecuteScalar()
End Sub
Private Sub Members_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Conn.Close()
End Sub
plz help
-
Apr 15th, 2010, 06:13 AM
#2
Re: Data isnt getting Saved, using oledb cmd
First up, ExecuteScalar is for retrieving a single value. Use ExecuteNonQuery and test its return value. If it's not zero then the data is being saved
-
Apr 15th, 2010, 07:18 AM
#3
Frenzied Member
-
Apr 16th, 2010, 05:48 AM
#4
Thread Starter
Frenzied Member
Re: Data isnt getting Saved, using oledb cmd
well thanks my original problem is solved, but
one question with respect to
one more thing:Its not a good idea to open a connection at the form load and closing the connection at the form closing
for more security to the database,just open the connection,perform executenonquery and then close the connection immediately
well if say every minute i want to get some data from the db, then should i connect and disconnect from the db every minute.
i also had an doubt on the same topic
If i have around 20 to 30 form which will access the same db , then do i need to create the connection object in each and every form
-
Apr 16th, 2010, 05:54 AM
#5
Frenzied Member
Re: Data isnt getting Saved, using oledb cmd
 Originally Posted by aashish_9601
well if say every minute i want to get some data from the db, then should i connect and disconnect from the db every minute.
you have to....whenever you are performing some database operations only for that moment of time you should be connected with the database else not ; though i dont know whether you will be performing some cryptographic security on the data present in your database or not
 Originally Posted by aashish_9601
If i have around 20 to 30 form which will access the same db , then do i need to create the connection object in each and every form
You have to
If you find my reply helpful , then rate it
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|