Click here to Skip to main content
15,889,211 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am able to store data in the database, but I am unable to update values in it. I have a table with 5 values, which are name, user, password, title, and project, and I need to be able to update the password, just by entering the user.
I tried this code:
String connString = System.Configuration.ConfigurationManager.ConnectionStrings["web"].ToString();
        conn = new MySql.Data.MySqlClient.MySqlConnection(connString);
        conn.Open();
        //queryStr = "";
        cmd = conn.CreateCommand();
        cmd.CommandType = System.Data.CommandType.Text;
        queryStr = "UPDATE meal.claves set usuario='" + user.Text+"where clave ='"+pass+"'";
        cmd = new MySql.Data.MySqlClient.MySqlCommand("update meal.claves Set usuario=@user,clave=@pass");
        //cmd = new MySql.Data.MySqlClient.MySqlCommand(queryStr, conn);
        //cmd.ExecuteReader();
            cmd.ExecuteNonQuery();
        
        conn.Close();

Got error saying connection must be open and valid.

What I have tried:

I tried the code in this video:
https://www.youtube.com/watch?v=sggbk2ES8DM
Posted
Updated 24-Nov-17 9:30am

Clue
C#
cmd = new ... 
. Your new cmd does not have a connection.
Also, do not concatenate strings to form sql command strings...use command parameters
 
Share this answer
 
C#
queryStr = "UPDATE meal.claves set usuario='" + user.Text+"where clave ='"+pass+"'";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
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