Table update with OnClick

ARC

Well-known member
Joined
Sep 9, 2006
Messages
63
Location
Minnesota
Programming Experience
Beginner
I'm trying to figure out how (or find a tutorial that explains how) to change data in an sql database table in the OnClick of a button, on an asp.net website project.

Im aware of the gridviews ability to allow modifying however, I dont want there to be user access to how it changes... I just want to hardcode into the button click event the changes to be made to the value in a certain table at a specific spot.

lets say... I have a table CoolPeople and it contains:

ID
Name
FavoriteNumber

for columns... with with each Onlcick, I want to add 100 to the FavoriteNumber value for the selected index of the gridview.

I've looked through some video tutorials, but couldnt seem to find how to issue the sql statement/using the connection string/what-have-you... in a button click event.

Thanks in advance!
 
I do not know if you are writing in VB or C# so I will put the example in VB. Also, you did not specify the database so I will use SQL Server.

private sub cmdSubmit_Click()

Dim sql as String

string s = "Put your connection string to your database
'Create Connection
Dim cn as New SQLConnection(cn)
'Create Command
Dim cmd as New SQLCommand()

sql = "Update CoolPeople "
sql += "Set FavoriteNumber = " & me.GridView1.SelectedRow.Cells[2].Text + 100 & " "
sql += "Where ID = " & me.GridView1.SelectedRow.Cells[0].Text & " "
sql += "And Name = '" & me.GridView1.SelectedRow.Cells[1].Text & "' "

cmd.Execute(sql)

End Sub
 
I do not know if you are writing in VB or C# so I will put the example in VB.
crvasquez, this is the VB.Net Forums - only VB.Net here :)

Use the & operator to add strings, + operator is for numbers and get you in trouble if you add strings and numbers also.

Also use Parameters when creating a Sql command, adding strings together can get you in all sorts of trouble. (It's like using reflection and Object type for all your code. which you don't.)
 

Latest posts

Back
Top