Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 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 7th, 2007, 05:37 PM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to rsearing
Default Extra Spaces Added When Inserting

This one has stumped me. I debug the following code (which is ran during Protected Sub CreateUserWizard1_CreatedUser) and I check the values of all my parameters...there are NO spaces. I put the DEBUG point right at the executenonquery statement. THEN, I quit debugging before the redirect and go to the DB and there are a bunch of spaces added to several columns.

Why?

(NOTE...it seems to always be roleStr & CouncilPosStr that it does it with--I even tried making the insertString without the parameters--coding the variables into the string..same thing)

(Also--here is the string copied DIRECTLY from Debug:

"INSERT INTO [members] (fname, lname, email, home_phone, cell_phone, address, city, state, zip, userName, role, password, councilPosition) VALUES ('Rob', 'Searing', '[email protected]', 'none', 'none', '1234 Some St', 'City', 'KS', '66213', 'rjsearing', 'No Role', 'password', 'none')"

)

Dim insertString = "INSERT INTO [members] (fname, lname, email, home_phone, cell_phone, address, city, state, zip, userName, role, password, councilPosition) VALUES (@fname, @lname, @email, @home, @cell, @address, @city, @state, @zip, @userNa, @rol, @pass, @councilPosStr)"
        Dim knightsDBConn As New SqlConnection(conString)
        Dim sqlCmd As New SqlCommand(insertString, knightsDBConn)
        sqlCmd.CommandType = CommandType.Text
        sqlCmd.Parameters.AddWithValue("@fname", firstNameStr)
        sqlCmd.Parameters.AddWithValue("@lname", lastNameStr)
        sqlCmd.Parameters.AddWithValue("@email", emailStr)
        sqlCmd.Parameters.AddWithValue("@home", phoneStr)
        sqlCmd.Parameters.AddWithValue("@cell", cellStr)
        sqlCmd.Parameters.AddWithValue("@address", addressStr)
        sqlCmd.Parameters.AddWithValue("@city", cityStr)
        sqlCmd.Parameters.AddWithValue("@state", stateStr)
        sqlCmd.Parameters.AddWithValue("@zip", zipStr)
        sqlCmd.Parameters.AddWithValue("@userNa", userNameStr)
        sqlCmd.Parameters.AddWithValue("@rol", "roleStr")
        sqlCmd.Parameters.AddWithValue("@pass", passwd)
        sqlCmd.Parameters.AddWithValue("@councilPosStr", councilPosStr)
        knightsDBConn.Open()
        sqlCmd.ExecuteNonQuery()
        sqlCmd = Nothing
        Response.Redirect("~/admin/addmember.aspx")
 
Old January 7th, 2007, 09:10 PM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to rsearing
Default

Ok..I figured it out..but not sure what I did...or rather, what the problem was.

I assumed it had to be something in the DB--so I looked at what I had typed the fields. Well, I changed them..and not sure what some were..but I changed some that were varchar(25) to nvarchar(25) and nchar to nvarchar...could this have been the problem?

If so...can someone please point me to an article that discusses the different data types?

Thanks,
Rob

 
Old January 8th, 2007, 09:43 AM
Registered User
 
Join Date: Jan 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The change from char to varchar (or as the case may be, nchar to nvarchar) will have made the difference. char and nchar datatypes automatically fill up to the indicated length with spaces, so if you put 'hello' in a char(10) field, it'll come out as 'hello '. varchar (and nvarchar) on the other hand do not pad strings that are shorter than the field's max length.

The difference between having the n or not is whether or not the value is stored as Unicode (requiring double space but supporting far more non-standard characters), and won't affect the above. Basically, you'll choose between nvarchar or varchar depending on whether you need Unicode support, and between varchar and char depending on whether you want your strings to be padded out with spaces. I understand there's a slight performance tradeoff (chars being slightly faster) but I've never seen that make a difference in practice, so I've always stuck to varchar myself. Alternatively if you do use char you'll probably have to trim any values before using them.
 
Old January 8th, 2007, 10:56 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to rsearing
Default

Thank you very much--that helps alot!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Diference between these two name spaces pradeepn XSLT 1 June 25th, 2007 02:31 AM
disappearing spaces gezi XSLT 0 March 26th, 2006 02:23 AM
spaces problem mateenmohd SQL Server 2000 8 August 11th, 2003 06:27 PM





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