Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics 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 January 11th, 2008, 12:23 PM
Authorized User
 
Join Date: Oct 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default can not add data into database

hi there,

i try to get data from textbox and add to dabase but it doesn't work, someone can help me ?

code behind..
Code:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.UI.WebControls
Imports System.Web.UI.Page
Imports System.Data.Sqlserver
Imports System.Data.Sql
Imports System.Data.SqlTypes



    Public Class sign_up : Inherits Page

    Public Result As Label
    Dim Conn As String = ConfigurationSettings.AppSettings("Conn")
    Dim MyCommand As SqlCommand
    Dim ch As String = "S"
    Dim myConnection As SqlConnection
    Public username_text, password_text, contact_name, postal_postcode, email, postal_suburb, postal_addr, mob_phone, home_phone, work_phone As System.Web.UI.WebControls.TextBox
    Public Message As System.Web.UI.WebControls.Label



    Function btnSignIn(ByVal username_text As String, ByVal password_text As String, ByVal contact_name As String, ByVal email As String, ByVal postal_postcode As String, ByVal postal_addr As String, ByVal postal_suburb As String, ByVal mob_phone As String, ByVal work_phone As String, ByVal home_phone As String) As Integer
        'Sub btnSignIn_Click(ByVal sender As Object, ByVal e As EventArgs)

        myConnection = New SqlConnection(Conn)

        Dim strSQLQuery As String = "Insert Into Seller( Username,Password,SellerName,Email,PostCode,Address,Suburb,MobilePhone,WorkPhone,HomePhone,Char) values ( @username_text, @password_text, @contact_name,@email,@postal_postcode,@postal_addr,@postal_suburb,@mob_phone,@work_phone,@home_phone,'S')"


        Dim SqlCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
        ' Dim SqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(strSQLQuery, myConnection)


        'SqlCommand.Parameters.Add("@username_text", System.Data.SqlDbType.Text).Value = username_text
        'SqlCommand.Parameters.Add("@password_text", System.Data.SqlDbType.Text).Value = password_text
        'SqlCommand.Parameters.Add("@contact_name", System.Data.SqlDbType.Text).Value = contact_name
        'SqlCommand.Parameters.Add("@email", System.Data.SqlDbType.Text).Value = email
        'SqlCommand.Parameters.Add("@postal_postcode", System.Data.SqlDbType.Text).Value = postal_postcode
        'SqlCommand.Parameters.Add("@postal_addr", System.Data.SqlDbType.Text).Value = postal_addr
        'SqlCommand.Parameters.Add("@postal_suburb", System.Data.SqlDbType.Text).Value = postal_suburb       
        'SqlCommand.Parameters.Add("@mob_phone", System.Data.SqlDbType.Text).Value = mob_phone
        'SqlCommand.Parameters.Add("@work_phone", System.Data.SqlDbType.Text).Value = work_phone
        'SqlCommand.Parameters.Add("@home_phone", System.Data.SqlDbType.Text).Value = home_phone







        SqlCommand.CommandText = strSQLQuery
        SqlCommand.Connection = myConnection

        '  Dim DbParam_username As IDataParameter = New SQLParameter

        Dim DbParam_username As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter()
        DbParam_username.ParameterName = "@username_text"
        DbParam_username.Value = Server.HtmlEncode(username_text)
        DbParam_username.DbType = System.Data.DbType.StringFixedLength
        SqlCommand.Parameters.Add(DbParam_username)

        Dim DbParam_password As IDataParameter = New SQLParameter
        DbParam_username.ParameterName = "@password_text"
        DbParam_username.Value = Server.HtmlEncode(password_text)
        DbParam_username.DbType = System.Data.DbType.StringFixedLength
        SqlCommand.Parameters.Add(DbParam_password)

        Dim DbParam_contactname As IDataParameter = New SQLParameter
        DbParam_username.ParameterName = "@contact_name"
        DbParam_username.Value = Server.HtmlEncode(contact_name)
        DbParam_username.DbType = System.Data.DbType.StringFixedLength
        SqlCommand.Parameters.Add(DbParam_contactname)

        Dim DbParam_postcode As IDataParameter = New SQLParameter
        DbParam_username.ParameterName = "@postal_postcode"
        DbParam_username.Value = Server.HtmlEncode(postal_postcode)
        DbParam_username.DbType = System.Data.DbType.StringFixedLength
        SqlCommand.Parameters.Add(DbParam_postcode)

        Dim DbParam_email As IDataParameter = New SQLParameter
        DbParam_username.ParameterName = "@email"
        DbParam_username.Value = Server.HtmlEncode(email)
        DbParam_username.DbType = System.Data.DbType.StringFixedLength
        SqlCommand.Parameters.Add(DbParam_email)

        Dim DbParam_suburb As IDataParameter = New SQLParameter
        DbParam_username.ParameterName = "@postal_suburb"
        DbParam_username.Value = Server.HtmlEncode(postal_suburb)
        DbParam_username.DbType = System.Data.DbType.StringFixedLength
        SqlCommand.Parameters.Add(DbParam_suburb)

        Dim DbParam_addr As IDataParameter = New SQLParameter
        DbParam_username.ParameterName = "@postal_addr"
        DbParam_username.Value = Server.HtmlEncode(postal_addr)
        DbParam_username.DbType = System.Data.DbType.StringFixedLength
        SqlCommand.Parameters.Add(DbParam_addr)

        'Dim DbParam_ch As IDataParameter = New SQLParameter
        'DbParam_username.ParameterName = "@ch"
        'DbParam_username.Value = Server.HtmlEncode(ch)
        'DbParam_username.DbType = System.Data.DbType.StringFixedLength
        'SqlCommand.Parameters.Add(DbParam_ch)

        Dim DbParam_mob As IDataParameter = New SQLParameter
        DbParam_username.ParameterName = "@mob_phone"
        DbParam_username.Value = Server.HtmlEncode(mob_phone)
        DbParam_username.DbType = System.Data.DbType.StringFixedLength
        SqlCommand.Parameters.Add(DbParam_mob)

        Dim DbParam_home As IDataParameter = New SQLParameter
        DbParam_username.ParameterName = "@work_phone"
        DbParam_username.Value = Server.HtmlEncode(work_phone)
        DbParam_username.DbType = System.Data.DbType.StringFixedLength
        SqlCommand.Parameters.Add(DbParam_home)

        Dim DbParam_work As IDataParameter = New SQLParameter
        DbParam_username.ParameterName = "@home_phone"
        DbParam_username.Value = Server.HtmlEncode(home_phone)
        DbParam_username.DbType = System.Data.DbType.StringFixedLength
        SqlCommand.Parameters.Add(DbParam_work)





        Dim rowsAffected As Integer = 0


        myConnection.Open()


        Try

            rowsAffected = sqlCommand.ExecuteNonQuery
            Response.Redirect("add_confirm.aspx")

        Finally

            myConnection.Close()

        End Try


        Return rowsAffected
    End Function

    Sub btnSignIn_Click(ByVal sender As Object, ByVal e As EventArgs)

        If Page.IsValid Then
            btnSignIn(username_text.Text, password_text.Text, contact_name.Text, email.Text, postal_postcode.Text, postal_addr.Text, postal_suburb.Text, mob_phone.Text, work_phone.Text, home_phone.Text)
            Response.Redirect("add_confirm.aspx")
        End If

    End Sub




End Class
 
Old January 11th, 2008, 01:55 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

do you see any error?? are you looking at the same database that you are inserting into?

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old January 11th, 2008, 02:07 PM
Authorized User
 
Join Date: Oct 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi, i dont see any error? it seems always return false b/c it always Redirect to ("noadd_confirm.aspx"). it's added to the same table, although there are some Null fields in the table but i think it's not a big deal b/c i set all null expt PK

 
Old January 11th, 2008, 02:16 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

mmm.. so rowsaffected is 0.. can you trace the code to see if the try..catch is catching?

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old January 11th, 2008, 02:33 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

I agree with Gonzalo, step through the code and see what is going on.

Also from a "best practice", IMHO, you shouldn't Redirect in a function.

The return value from the function should dictate program flow in the calling method.

My 2 cents anyway!

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========
 
Old January 11th, 2008, 03:34 PM
Authorized User
 
Join Date: Oct 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi there,

thank 4 ur reply

when i try my code in inline style with comment lines, it worked. However when i tried it in code behind, it displayed this message, searching in google, i know wat it means but it seems i can not fix :( , can you help me to look throuhg it ?

Must declare the scalar variable "@username_text".
 
Old January 12th, 2008, 11:39 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

well.. where in your code is that???

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
add new datasource doesn't have database option aflores207 Visual C++ 0 August 19th, 2008 03:33 PM
Cant Add New Data wb8bgy Visual Basic 2005 Basics 3 February 21st, 2007 07:12 AM
How to add image in database desai Pramod ASP.NET 1.0 and 1.1 Basics 4 December 26th, 2006 07:52 AM
How to add to datagrid without database annsary ASP.NET 1.0 and 1.1 Professional 17 April 3rd, 2006 08:44 PM
ASP to add to Database gilgalbiblewheel Classic ASP Databases 7 September 3rd, 2004 06:43 PM





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