954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help needed

Can somebody help me here? i have managed to import excel data into a datagridview but am having problems saving and updating it. It is saving alright but when it comes to updating that's when i have problems.

here is my code:

If mysqldr.HasRows Then
mysqldr.Close()

newcommand = New MySqlCommand(" Update item_stock set Item_Name = '" &  Replace(itemname, "'", "''") & "' ,Sale_Price = "' & Retailprice &"'" , newconnection) newcommand.ExecuteNonQuery()
 
 Else
mysqldr.Close()
sqrs = "insert into item_stock(Item_Name,Vehiclename,VAT_Rate,Supp_Name,Binno,Gearboxtype,Superseedingno,Engineno,Partno,oemname,model,Qty,Buyingprice,Sale_Price)values('" & _
 Replace(itemname, "'", "''") & "','" & vname & "',16,'" & suppname & "'" )"
 newcommand = New MySqlCommand(sqrs, newconnection)
newcommand.ExecuteNonQuery()
Me.ProgressBar1.Increment(id)
End If
id = id + 1
ProgressBar1.Maximum = DataGridView1.RowCount - 1


if somebody can help me out i would really appreciate it.Please......... :-)

Steve Mac
Newbie Poster
12 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Always use parameterized queries.

Sample:

Dim cn As New MySqlConnection("Server=localhost;Database=testdb;Uid=root;Pwd=;")
        Dim cmd As New MySqlCommand
        cmd.CommandText = "update testTable set col1=?p1, col2=?p2 where col3=?p3"
        cmd.Connection = cn
        cmd.Parameters.AddWithValue("?p1", value1)
        cmd.Parameters.AddWithValue("?p2", value2)
        cmd.Parameters.AddWithValue("?p3", value3)
        cn.Open()
        cmd.ExecuteNonQuery()
        cn.Close()
__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

Always use parameterized queries.

Sample:

Dim cn As New MySqlConnection("Server=localhost;Database=testdb;Uid=root;Pwd=;")
        Dim cmd As New MySqlCommand
        cmd.CommandText = "update testTable set col1=?p1, col2=?p2 where col3=?p3"
        cmd.Connection = cn
        cmd.Parameters.AddWithValue("?p1", value1)
        cmd.Parameters.AddWithValue("?p2", value2)
        cmd.Parameters.AddWithValue("?p3", value3)
        cn.Open()
        cmd.ExecuteNonQuery()
        cn.Close()


This approach works but my problem is that i want to search for the values in the database, if they exist i would like to update them but if they dont then i would like to insert them.Thanks for your help.

Steve Mac
Newbie Poster
12 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

What I am suggesting that you have to rewrite code using Parameter. If you're stuck post what you have.

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

Thanks man, i have done that and it has actually worked! Cheers Man! thanks!

Steve Mac
Newbie Poster
12 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You