Click here to Skip to main content
15,915,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

How to use AddWithValue for updating statement? For insert new data, it's ok. But for updating, it not updating the arrImg and arrLogo where it become null on mysql. Below is my code for updating. please see and guide me.

SQL
strSQL = "UPDATE malldir.tbldirectory SET dirno=@dirno,dirname=@dirname,dirdetails=@dirdetails,category=@category,level=@level,lotno=@lotno,website=@website,phoneno=@phoneno,dirinitial=@dirinitial,dir_image=@dirimage,dir_logo=@dirlogo WHERE dirname='" & strcTmpDir.strShopOldName & "';"

            mysqlconn = New MySqlClient.MySqlConnection(MyConnectionString)
            mysqlconn.Open()
            With mysqlcmd
                .CommandText = strSQL
                .Connection = mysqlconn
                .Parameters.AddWithValue("@dirno", strcTmpDir.strShopNo)
                .Parameters.AddWithValue("@dirname", strcTmpDir.strShopName)
                .Parameters.AddWithValue("@dirdetails", strcTmpDir.strShopDetails)
                .Parameters.AddWithValue("@category", strcTmpDir.strCategory)
                .Parameters.AddWithValue("@level", strcTmpDir.strFloor)
                .Parameters.AddWithValue("@lotno", strcTmpDir.strLotNo)
                .Parameters.AddWithValue("@website", strcTmpDir.strWebsite)
                .Parameters.AddWithValue("@phoneno", strcTmpDir.strPhoneNo)
                .Parameters.AddWithValue("@dirinitial", strcTmpDir.strInit)
                .Parameters.AddWithValue("@dirimage", strcTmpDir.arrImg)
                .Parameters.AddWithValue("@dirlogo", strcTmpDir.arrLogo)
                intRec = .ExecuteNonQuery()
            End With


Is my sql statement is good?any suggestion?
Posted

1 solution

you may need to handle null values like below

.Parameters.AddWithValue("@dirimage", (object)strcTmpDir.arrImg?? (object)DBNull.Value)

and also use parameters for all the values (you haven't use parameter for dirname)
You need to wrap the Command and the Connection in using statements
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900