PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] VS 2015 is there a better and cleaner way to do this?-VBForums
Results 1 to 13 of 13

Thread: [RESOLVED] VS 2015 is there a better and cleaner way to do this?

  1. #1

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    4,628

    Resolved [RESOLVED] VS 2015 is there a better and cleaner way to do this?

    hey i wrote this code and not only i have a error in this line of code

    Code:
    " @CardRemarks@permanentCustomer@Photo@Optional" &

    but the code is pretty messy i think
    is there a cleaner or nicer way to wright this?

    Code:
               Cmd.CommandText = "insert into Customers (Fullname,address,DateBirth," &
                "FaceBook,Email,Phone,Cellular,CardRemarks," &
                " permanentCustomer,Photo,Optional,PayMents,Gender," &
                " SmsSended,SmsPermision,CameFrom,Ariveddate,City)" &
                " Values (@Fullname,@address,@DateBirth,@FaceBook,@Email,@Phone,@Cellular" &
                " @CardRemarks@permanentCustomer@Photo@Optional" &
                " @PayMents@Gender@Photo@Optional" &
                " @CardRemarks@permanentCustomer@SmsSended@SmsPermision" &
                " @CameFrom@Ariveddate@City@Optional)"
    
                Cmd.Parameters.AddWithValue("@Fullname", txtname.Text)
                Cmd.Parameters.AddWithValue("@address", txtadress.Text)
                Cmd.Parameters.AddWithValue("@DateBirth", datebirthpick.Value)
                Cmd.Parameters.AddWithValue("@FaceBook", txtfacebook.Text)
                Cmd.Parameters.AddWithValue("@Email", txtemail.Text)
                Cmd.Parameters.AddWithValue("@Phone", txtphone.Text)
                Cmd.Parameters.AddWithValue("@Cellular", txtcelular.Text)
                Cmd.Parameters.AddWithValue("@CardRemarks", txtremarks.Text)
                Cmd.Parameters.AddWithValue("@permanentCustomer", CmbPermanentCust.Text)
                Cmd.Parameters.AddWithValue("@Photo", StrPhoto)
                Cmd.Parameters.AddWithValue("@Optional", StrPhoto)
                Cmd.Parameters.AddWithValue("@PayMents", 0)
                Cmd.Parameters.AddWithValue("@Gender", CmbGender.Text)
                Cmd.Parameters.AddWithValue("@SmsSended", 0)
                Cmd.Parameters.AddWithValue("@SmsPermision", CmbSms.Text)
                Cmd.Parameters.AddWithValue("@CameFrom", CmbCameFrom.Text)
                Cmd.Parameters.AddWithValue("@Ariveddate", datearrived.Text)
                Cmd.Parameters.AddWithValue("@City", CmbCity.Text)
                Cmd.ExecuteNonQuery()
    tnx for any help
    salsa

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,624

    Re: VS 2015 is there a better and cleaner way to do this?

    You have no commas separating the parameters in the line you showed. You have commas separating the parameters prior to that, but not on that line or the next.
    My usual boring signature: Nothing

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,383

    Re: VS 2015 is there a better and cleaner way to do this?

    There are a number of changes that I'd make to that code. Firstly, there's no good reason to use string concatenation to build that SQL code. Doing so just makes it harder to read and thus more error-prone. If you're using VB 2015 or later then you can write a multiline String literal:
    vb.net Code:
    1. Dim sql = "INSERT INTO Customers
    2.           (
    3.               Fullname,
    4.               address,
    5.               DateBirth,
    6.               FaceBook,
    7.               Email,
    8.               Phone,
    9.               Cellular,
    10.               CardRemarks,
    11.               permanentCustomer,
    12.               Photo,
    13.               Optional,
    14.               PayMents,
    15.               Gender,
    16.               SmsSended,
    17.               SmsPermision,
    18.               CameFrom,
    19.               Ariveddate,
    20.               City
    21.           )
    22.           VALUES
    23.           (
    24.               @Fullname,
    25.               @address,
    26.               @DateBirth,
    27.               @FaceBook,
    28.               @Email,
    29.               @Phone,
    30.               @Cellular,
    31.               @CardRemarks,
    32.               @permanentCustomer,
    33.               @Photo,
    34.               @Optional,
    35.               @PayMents,
    36.               @Gender,
    37.               @Photo,
    38.               @Optional,
    39.               @CardRemarks,
    40.               @permanentCustomer,
    41.               @SmsSended,
    42.               @SmsPermision,
    43.               @CameFrom,
    44.               @Ariveddate,
    45.               @City,
    46.               @Optional
    47.           )"
    I don't know about you but I find that much easier to read and thus much easier to see where you've made the mistakes. I've fixed the one that Shaggy mentioned, i.e. the missing commas, but I have left the other one, i.e. your value list doesn't match your column list.

    On an unrelated note, while I have used AddWithValue myself in the past, I've come to realise that it can be problematic. The inferred data type is not always correct so it is considered good practice to use Add instead, in which case you can specify the correct data type explicitly and then set the Value property, e.g.
    vb.net Code:
    1. Cmd.Parameters.Add("@Fullname", SqlDbType.VarChar, 50).Value = txtname.Text
    Also, you might consider using a With block to add the parameter, so you don't have to repeat Cmd.Parameters over and over.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    4,628

    Re: VS 2015 is there a better and cleaner way to do this?

    you might consider using a With block to add the parameter,
    im new in vb net
    what do you mean?

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,383

    Re: VS 2015 is there a better and cleaner way to do this?

    Quote Originally Posted by salsa31 View Post
    im new in vb net
    But, with 4582 posts, clearly not new to development and clearly not new to using the internet. What is stopping you from doing a web search to answer that question for yourself? Apart from that, it's a VB.NET feature that is inherited from VB6, which you're also not new to.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    4,628

    Re: VS 2015 is there a better and cleaner way to do this?

    But, with 4582 posts, clearly not new to development


    well i did as you said and left the addwithvalue
    Code:
                Cmd.Parameters.Add("@Fullname", SqlDbType.NVarChar, 100).Value = txtname.Text
                Cmd.Parameters.Add("@address", SqlDbType.NVarChar, 100).Value = txtadress.Text
                Cmd.Parameters.Add("@DateBirth", SqlDbType.Date).Value = datebirthpick.Value
                Cmd.Parameters.Add("@FaceBook", SqlDbType.NVarChar, 50).Value = txtname.Text
                Cmd.Parameters.Add("@Email", SqlDbType.NVarChar, 100).Value = txtname.Text
                Cmd.Parameters.Add("@Phone", SqlDbType.NVarChar, 10).Value = txtname.Text
                Cmd.Parameters.Add("@Cellular", SqlDbType.NVarChar, 10).Value = txtname.Text
                Cmd.Parameters.Add("@CardRemarks", SqlDbType.NVarChar, 1000).Value = txtname.Text
                Cmd.Parameters.Add("@permanentCustomer", SqlDbType.NVarChar, 50).Value = txtname.Text
                Cmd.Parameters.Add("@Photo", SqlDbType.VarChar, 500).Value = txtname.Text
                Cmd.Parameters.Add("@Optional", SqlDbType.VarChar, 50).Value = txtname.Text
                Cmd.Parameters.Add("@PayMents", SqlDbType.Money).Value = "0"
                Cmd.Parameters.Add("@Gender", SqlDbType.VarChar, 50).Value = txtname.Text
                Cmd.Parameters.Add("@SmsSended", SqlDbType.VarChar, 50).Value = "0"
                Cmd.Parameters.Add("@SmsPermision", SqlDbType.VarChar, 50).Value = txtname.Text
                Cmd.Parameters.Add("@CameFrom", SqlDbType.VarChar, 50).Value = txtname.Text
                Cmd.Parameters.Add("@Ariveddate", SqlDbType.Date).Value = datearrived.Value
                Cmd.Parameters.Add("@City", SqlDbType.VarChar, 50).Value = txtname.Text

  7. #7

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    4,628

    Re: VS 2015 is there a better and cleaner way to do this?

    tnx for the Help

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,383

    Re: VS 2015 is there a better and cleaner way to do this?

    [QUOTE=salsa31;5331205]well i did as you said and left the addwithvalue/QUOTE]

    It does make for more verbose code but it is worth it. AddWithValue works most of the time but I have encountered items when it doesn't and the fact that they are rare makes it easier for them to sneak through.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    4,628

    Re: VS 2015 is there a better and cleaner way to do this?

    [QUOTE=jmcilhinney;5331209]
    Quote Originally Posted by salsa31 View Post
    well i did as you said and left the addwithvalue/QUOTE]

    It does make for more verbose code but it is worth it. AddWithValue works most of the time but I have encountered items when it doesn't and the fact that they are rare makes it easier for them to sneak through.
    i trust you

  10. #10
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,369

    Re: VS 2015 is there a better and cleaner way to do this?

    Quote Originally Posted by jmcilhinney View Post

    On an unrelated note, while I have used AddWithValue myself in the past, I've come to realise that it can be problematic. The inferred data type is not always correct so it is considered good practice to use Add instead, in which case you can specify the correct data type explicitly and then set the Value property, e.g.
    vb.net Code:
    1. Cmd.Parameters.Add("@Fullname", SqlDbType.VarChar, 50).Value = txtname.Text
    Also, you might consider using a With block to add the parameter, so you don't have to repeat Cmd.Parameters over and over.
    I haven't encounterd a problem with -AddWithValue-
    but it's good to know that there can be problem(s), thanks for sharing

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,624

    Re: [RESOLVED] VS 2015 is there a better and cleaner way to do this?

    I have never encountered issues with AddWithValue, either, but it is clear how it COULD happen. There's a bit of implicit conversion that can happen with that when what you supply is kind of like the type in the DB, but not exactly. Some changes could happen, which may not be what you expect.
    My usual boring signature: Nothing

  12. #12
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,369

    Re: [RESOLVED] VS 2015 is there a better and cleaner way to do this?

    Quote Originally Posted by Shaggy Hiker View Post
    I have never encountered issues with AddWithValue, either, but it is clear how it COULD happen. There's a bit of implicit conversion that can happen with that when what you supply is kind of like the type in the DB, but not exactly. Some changes could happen, which may not be what you expect.
    Hi,

    perhaps JMC can explain more what and with which type
    I guess with Boolean, they can be a pain
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  13. #13
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,489

    Re: [RESOLVED] VS 2015 is there a better and cleaner way to do this?

    I’ve had issues with AddWithValue when using the OleDb engine and date columns. I ultimately had to use Add while specifying the OleDb type and then assigning the value.

    Edit - I posted this last night off my cell phone, but now that I'm at my desk I can elaborate. It resulted in a data mismatch error because the DateTime object didn't directly match up with the column's data type. So ultimately I was forced to do something like this:
    Code:
    .Add("@time", OleDbType.DBTimeStamp).Value = DateTime.Now()
    Last edited by dday9; Nov 9th, 2018 at 09:38 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width