User Input Not Updating In New dgv Row

Visual Basic 2008 Express and MySQL

Page 1 of 1

3 Replies - 760 Views - Last Post: 05 November 2009 - 11:06 AM Rate Topic: -----

#1 charliechilds  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 30-October 09

User Input Not Updating In New dgv Row

Post icon  Posted 04 November 2009 - 12:43 PM

Hello Folks,

I have followed a tutorial at http://www.vbmysql.c...mysql-tutorials. It is a very good tutorial but it is also a little dated. Most people who visit it praise it to high heaven so I decided it was a good place to start as an absolute beginner to programming. (I have teamed it up with Dan Mabbut Vis basic tutorial at about.com and joined DreamInCode in order to learn what I can accomplish with Visual Basic 2008 Express ). I am up to part 6 of 7 but keep going back to part 4 because there is one aspect I just cant work out.
I am creating a form which logs staff in or out of work and allows staff to include a message with their ( at work /away from work ) status details on the form.

Everything works as it should apart from one thing:

The form is supposed to update a users status in a new row when they click a button. In my form the rows per user are being over-written with the updated information - it is not being written in a new row. There is a login form which precedes the staff status form - this has a little relevant code re calling the user info from the database but I think my problem is related to the insert statement in the middle of the below code.



Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
				Dim conn As New MySqlConnection
				Dim myCommand As New MySqlCommand

				conn.ConnectionString = myConnString

				myCommand.Connection = conn
 <!--HERE-->  
myCommand.CommandText = "INSERT INTO event(user_id, message_id, timestamp, status, creator)" _
				 & "VALUES(?UserID, ?MessageID, NOW(), ?Status, ?Creator)"

				myCommand.Parameters.Add("?UserID", myUserID)
				myCommand.Parameters.Add("?MessageID", cboMessage.SelectedValue)
				myCommand.Parameters.Add("?Status", cboStatus.SelectedItem)
				myCommand.Parameters.Add("?Creator", myUserID)

				Try
						conn.Open()
						myCommand.ExecuteNonQuery()
				Catch myerror As MySqlException
						MsgBox("There was an error updating the database: " & myerror.Message)
				End Try
		End Sub




My code has changed quite a bit since the above as the form has progressed but the problem started at the stage above and has not changed with the forms progression. Every other aspect of staff update info is working well in my form - just not in a new row.

I am going bald - The tutorial was written for VB2005 Express - I am using VB2008 Express though it has not made too much difference to the tutorial. Does anyone know if any changes have been made to Visual Basic 2008 Express which might answer my question or does anyone have any other ideas of what may be wrong.

Being my first lesson I would really like to finish it properly. I am very grateful for any help.

Many thanks,
Charlie
(Hopefully will be starting one of DreamInCodes tutorials v soon - am currently searching through all your beginners posts for help with learning ideas.)

Is This A Good Question/Topic? 0
  • +

Replies To: User Input Not Updating In New dgv Row

#2 Sethro117  Icon User is offline

  • Still the sexiest mofo.
  • member icon

Reputation: 236
  • View blog
  • Posts: 2,378
  • Joined: 14-January 09

Re: User Input Not Updating In New dgv Row

Posted 04 November 2009 - 02:22 PM

Try using an UPDATE statement instead of an INSERT.
UPDATE event SET status=?Status WHERE user_id='" & myuserid & "'"
myCommand.Parameters.Add("?Status", cboStatus.SelectedItem)


EDIT: Youll also need to close your conection and update the DGV. Let us know if you have any problems with that.

This post has been edited by Sethro117: 04 November 2009 - 02:26 PM

Was This Post Helpful? 0
  • +
  • -

#3 charliechilds  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 30-October 09

Re: User Input Not Updating In New dgv Row

Posted 05 November 2009 - 10:58 AM

Hi Seth,

Thanks for your feedback.

In all honesty I think I am in over my head with this as I am an absolute beginner. I hope I am not a pain when I explain the following.

I deleted the INSERT statement and placed your UPDATE statement in its place.

UPDATE event SET status=?Status WHERE user_id='" & myuserid & "'"
		myCommand.Parameters.AddWithValue("?Status", cboStatus.SelectedItem)


I get a blue squiggle under the word "event". No other squiggles.
There are 2 error warnings for that same line of code.
1) Expression expected
2)Method arguments must be enclosed in parentheses

I tried putting everything after the "=" sign in parentheses but no joy there.

When building I get a "Build Failed" notification.

Re your additional "EDIT" - I took that to mean that I should close the project and re-open it. I am not sure what you meant by "update the DGV unless you simply meant hit the update button which I couldn't do because the program would not run.

I would like to make this work as expected but it is not the ned of the world if I cant. I don't need the application for anything - just building it for learning experience. I have read the tutorial over and over and feel that I have gained a lot but still need to get to grips with the basic understanding of how to code this stuff.

Would you know of a tutorial for VB.NET or Visual Basic 2008 Express which is written for people with zero programming experience?

Thanks again
Was This Post Helpful? 0
  • +
  • -

#4 Sethro117  Icon User is offline

  • Still the sexiest mofo.
  • member icon

Reputation: 236
  • View blog
  • Posts: 2,378
  • Joined: 14-January 09

Re: User Input Not Updating In New dgv Row

Posted 05 November 2009 - 11:06 AM

You need the UPDATE statement in "" Just like your iINSERT statement. Sorry for the confusion. Also, what I meant by the close your connection is you need to close youre connection. So itll look like this:
				Try
						conn.Open()
						myCommand.ExecuteNonQuery()
conn.Close() ' This is the line to close the connection.
				Catch myerror As MySqlException
						MsgBox("There was an error updating the database: " & myerror.Message)
				End Try

This post has been edited by Sethro117: 05 November 2009 - 11:12 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1