Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB.NET
Private Sub updateww()
        Dim con As New SqlConnection
        Dim cmd As New SqlCommand

        con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Administrator\Documents\Visual Studio 2010\Projects\St. Peter Academy\St. Peter Academy\App_Data\St.mdf;Integrated Security=True;User Instance=True"
        con.Open()
        cmd.Connection = con
        cmd.CommandText = "update " & dbname.Text & " set percentage='" & edwwp.Text & "', description='" & edwwd.Text & "' where componentid=1 "
        cmd.ExecuteNonQuery()
        con.Close()

    End Sub

    Private Sub updatept()
        Dim con As New SqlConnection
        Dim cmd As New SqlCommand

        con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Administrator\Documents\Visual Studio 2010\Projects\St. Peter Academy\St. Peter Academy\App_Data\St.mdf;Integrated Security=True;User Instance=True"
        con.Open()
        cmd.Connection = con
        cmd.CommandText = "update " & dbname.Text & " set percentage= '" & edptp.Text & "', description='" & edptd.Text & "' where componentid=2 "
        cmd.ExecuteNonQuery()
        con.Close()

    End Sub
    Private Sub updateqa()
        Dim con As New SqlConnection
        Dim cmd As New SqlCommand

        con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Administrator\Documents\Visual Studio 2010\Projects\St. Peter Academy\St. Peter Academy\App_Data\St.mdf;Integrated Security=True;User Instance=True"
        con.Open()
        cmd.Connection = con
        cmd.CommandText = "update " & dbname.Text & " set percentage='" & edqap.Text & "', description='" & edqad.Text & "' where componentid=3 "
        cmd.ExecuteNonQuery()
        con.Close()

    End Sub


What I have tried:

my update isnt working . it does not show any errors but not updating
Posted
Updated 23-Dec-16 23:22pm
Comments
Dave Kreskowiak 24-Dec-16 1:16am    
Without knowing the values that you're plugging into the query it's impossible to tell you.

I can say that you're using string concatenation to build the queries which opens your database to SQL Injection attacks and the complete destruction of your database.

Google for "SQL Injection attack" to find out why what you're doing is so bad.

Then Google for "VB.NET parameterized queries" to find out how to fix it AND make your code easier to debug.

So many things to fix here....

The big one is: don't do it like that. Never concatenate strings to form an SQL command - it leaves you wide open to SQL Injection attacks where your users can damage or destroy your database just by going in textbox. Always use parameterized queries instead.

Secondly, do not hard code connection strings - always store them in a config file or similar so you don't have to change the program between development and release.

Thirdly, don't routinely attach databases - let SQL handle them itself instead - attaching is an Express version only and is a special developer mode that is much slower than SQL management.

Fourthly, SqlConnection and SqlCommand objects are scarce resources and should be Disposed when you are finished with them.

Now for the problem you have noticed ... We can't tell! It could be you data in the textboxes is causing a problem, it could be that there are no errors which match your condition. So start be fixing the other stuff throughout your application first, and if the problem is still happening after that, check your DB and make sure that the data is where you think it is, and what you think it is.

But if you don't fix the other stuff first, your DB will get destroyed - your best mate will do it just to see the look on your face...
 
Share this answer
 
As said there are a lot of problems in the code
- concantenation of values leaves you open to SQL injection
- concantenation of values introduces conversion problems
- you don't use using blocks so Dispose may be omitted even if present
- you don't have any error handling
- connection string is statically embedded into a method
- not necessarily a problem but if these methods are used in a loop or with other DML statements then you're missing transactions and so on...

I suggest going through Properly executing database operations[^]
 
Share this answer
 

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