Print Page | Close Window

Inserting a New Record.

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=15345
Printed Date: 29 March 2026 at 8:40am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Inserting a New Record.
Posted By: davidshq
Subject: Inserting a New Record.
Date Posted: 02 June 2005 at 2:22pm
    Right now the database uses a DetailView to show one record. The person then has the opportunity to select that one record as being "used". When this occurs the program needs to pull information from the one table (Games) and insert the information into a different table (LastPlayed). Here is my current code:
Dim key As DataKey = DetailsView1.DataKey
Dim varid As String
varid = key.Value.ToString()
Label1.Text = key.Value.ToString()
Dim sql As String
sql = "INSERT INTO [LastPlayed] (uID, gID, LastPlayedDate) VALUES (" & Profile.UserName & ", " & varid & ", " & DateTime.Now & ")"
SqlDataSource1.InsertCommand = sql
    Anyways, I'm just generally over my head in this area, any websites w/documentation on SQL in ASP.NET 2.0 would be appreciated as well as specific tips on how to handle this code.
David.


-------------
- http://www.davemackey.net/" rel="nofollow - Dave Mackey - Virtual Home.



Replies:
Posted By: davidshq
Date Posted: 03 June 2005 at 5:18pm
To simplify my question....
   How can one update a database without using GridView, DetailsView, etc. Shouldn't one be able to do something like this:
SqlDataSource.InsertCommand = "INSERT INTO (tName) values ('james')"
SqlDataSource.ExecuteNonQuery()
David.


-------------
- http://www.davemackey.net/" rel="nofollow - Dave Mackey - Virtual Home.


Posted By: Mart
Date Posted: 03 June 2005 at 5:46pm
Just been messing about with the Data Access Model of .NET 2.0, this might help:

Create a new class with similar code (I'm gonna call my class TestClass):

Public Sub InsertData(string name)
Dim conn As New SqlConnection("...")
'Blah blah blah
'Execute the insert statement
End Sub

Then drop a ObjectDatasource into your page, change the TypeName property to TestClass, then change the InsertMethod to InsertData and click on the InsertParameters...

Add a new Profile parameter and set the property...

Then when your ready just go ObjectDatasource1.Insert();


Posted By: davidshq
Date Posted: 06 June 2005 at 10:21am
Here is my new code that now works:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim key As DataKey = DetailsView1.DataKey
        Dim varid As String
        varid = key.Value.ToString()
        Label1.Text = key.Value.ToString()
        Dim username As String
        username = Profile.UserName
        Dim sql As String
        sql = "INSERT INTO LastPlayed (uID, gID, LastPlayedDate) VALUES ('" & username & "', '" & varid & "', '" & DateTime.Now & "')"
        SqlDataSource1.InsertCommand = sql
        SqlDataSource1.Insert()
End Sub


-------------
- http://www.davemackey.net/" rel="nofollow - Dave Mackey - Virtual Home.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net