Wrox Programmer Forums
|
BOOK: Beginning ASP.NET Dynamic Websites w/ Web Matrix
This is the forum to discuss the Wrox book Beginning Dynamic Websites: with ASP.NET Web Matrix by Dave Sussman, James Greenwood, Alex Homer, Colt Kwong, John M. West; ISBN: 9780764543746
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET Dynamic Websites w/ Web Matrix section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 29th, 2004, 12:53 PM
Registered User
 
Join Date: Dec 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Beginner: Insert/Delete/Update Query method

Hi all,
am a beginner learning asp.net/msde..
i have a dbs which i am trying to insert a row to using the web matrix insert query method..

the code generated is as below..
Function MyInsertMethod() As Integer
        Dim connectionString As String = "server='(local)\netsdk'; trusted_connection=true; Database='CAM'"
        Dim sqlConnection As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(connectionStri ng)

        Dim queryString As String = "INSERT INTO [Discs] ([Title], [ReleaseDate], [Notes], [Label], [CoverImageURL]) V"& _
"ALUES (hellosir, getdate(), hellosir, hellosir, hellosir)"
        Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)

        Dim rowsAffected As Integer = 0
        sqlConnection.Open
        Try
            rowsAffected = sqlCommand.ExecuteNonQuery
        Finally
            sqlConnection.Close
        End Try

        Return rowsAffected
    End Function

what i did was, put the values of the data instead of using variables straight into the query method.. now what i am assuming is that if i run a page with this function in, it should update teh database rite?? well it don't.. and i am stuck.. i don't get any error messages, when i run the page i just get a blank white page, obviously as there isn't anything on the page.. but it should update teh database shouldn it??
any help much appreciated..
ty in advance..

 
Old December 29th, 2004, 01:18 PM
Registered User
 
Join Date: Dec 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok i figured how to do an insert...
went thru the usual web matrix insert query method, then added four text boxes, one for each field of the database that wasn't auto generated, and a button, in the event tag of the button added this bit of code..

Sub Button1_Click(sender As Object, e As EventArgs)
MyInsertMethod(TextBox1.Text,TextBox2.Text,TextBox 3.Text,TextBox4.Text)
end sub


followed by the auto generated insert function..

Function MyInsertMethod(ByVal title As String, ByVal notes As String, ByVal label As String, ByVal coverImageURL As String) As Integer
        Dim connectionString As String = "server='(local)\netsdk'; trusted_connection=true; Database='CAM'"
        Dim sqlConnection As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(connectionStri ng)

        Dim queryString As String = "INSERT INTO [Discs] ([Title], [ReleaseDate], [Notes], [Label], [CoverImageURL]) V"& _
"ALUES (@Title, getdate(), @Notes, @Label, @CoverImageURL)"
        Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)

        sqlCommand.Parameters.Add("@Title", System.Data.SqlDbType.VarChar).Value = title
        sqlCommand.Parameters.Add("@Notes", System.Data.SqlDbType.Text).Value = notes
        sqlCommand.Parameters.Add("@Label", System.Data.SqlDbType.VarChar).Value = label
        sqlCommand.Parameters.Add("@CoverImageURL", System.Data.SqlDbType.VarChar).Value = coverImageURL

        Dim rowsAffected As Integer = 0
        sqlConnection.Open
        Try
            rowsAffected = sqlCommand.ExecuteNonQuery
        Finally
            sqlConnection.Close
        End Try

        Return rowsAffected
    End Function


and hey presto it works.. so now any ideas how do i make delete work as such.. do i have to compare two fields from an input field??


 
Old February 28th, 2005, 02:09 PM
Registered User
 
Join Date: Feb 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to dark_blacky
Default

Hi
i have a problem with this Error

Server Error in '/' Application.
--------------------------------------------------------------------------------

String or binary data would be truncated. The statement has been terminated.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: String or binary data would be truncated. The statement has been terminated.

Source Error:


Line 755: dbConnection.Open
Line 756: Try
Line 757: rowsAffected = dbCommand.ExecuteNonQuery
Line 758: Finally
Line 759: dbConnection.Close

Help me please

PS. i speak & write only spanish






Similar Threads
Thread Thread Starter Forum Replies Last Post
Image -- Insert/Update/Delete in sql2000 !!! dagad ASP.NET 2.0 Basics 0 October 27th, 2006 08:25 AM
INSERT, UPDATE, DELETE dagad ASP.NET 2.0 Professional 0 September 27th, 2006 01:22 AM
Views-Insert/update/delete lily611 SQL Language 2 November 29th, 2004 03:41 AM
Update, insert and delete Trigger khautinh SQL Server 2000 2 September 17th, 2003 11:45 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.