Does somebody know How I can change values in a line in my database table?
EXAMPLE:
My Databese Table:
| No | Username | Tickets |
| 1 | User1 | 3 |
| 2 | User2 | 1 |
NOW: The User2 gets 4 tickets more.
So i have to ask how much tickets he have NOW, and than i have to add 4 tickes.
So he have 5 tickes.
1. Get the line, where 'Username' = 'User2'
2. Get the value of the column 'Tickets' of the line where 'Username' = 'User2'
3. add 4 tickets to the value of the column 'Tickets' of th line where 'Username' = 'User2'
But how I can do it?

Shivanand ArurRank #920 · 2.3K reputationPosted Sep 30, 2012, 10:28 AM EDT
"Select tickets from test where id = '" + trade.OtherSID + "'";
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 29, 2012, 10:43 AM EDT
meinenr97 reputationPosted Sep 29, 2012, 10:46 AM EDT
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 29, 2012, 10:49 AM EDT
meinenr97 reputationPosted Sep 29, 2012, 10:51 AM EDT
But I am able to change it, if you want.
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 29, 2012, 10:53 AM EDT
meinenr97 reputationPosted Sep 29, 2012, 11:00 AM EDT
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 29, 2012, 11:05 AM EDT
meinenr97 reputationPosted Sep 29, 2012, 11:21 AM EDT
Line: TicketValue = Convert.ToInt32(dr[0].ToString());
-> The name 'dr' does not exist in the ccurrent context
I have make something wrong?
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 29, 2012, 11:24 AM EDT
meinenr97 reputationPosted Sep 29, 2012, 11:30 AM EDT
I have used this script:
string myConnectionString = "SERVER=localhost;" + "DATABASE=test;" + "UID=root;" + "PASSWORD=;";
MySqlConnection connection = new MySqlConnection(myConnectionString);
MySqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT * FROM test";
MySqlDataReader Reader;
connection.Open();
Reader = command.ExecuteReader();
int TicketValue = 0;
while (Reader.Read())
{
TicketValue = Convert.ToInt32(dr[0].ToString());
string row = "";
for (int i = 0; i < Reader.FieldCount; i++)
row += Reader.GetValue(i).ToString() + ", ";
Console.WriteLine(row);
}
Reader.Close();
TicketValue = TicketValue + 4;
command.CommandText = "Select 1 from test where ID = '" + trade.OtherSID + "'";
Reader = command.ExecuteReader();
if(Reader.HasRows)
{
PrintConsole("ALREADY EXIST!!", ConsoleColor.Red);
command.CommandText = "Update test Set tickets = '" + TicketValue + "'" + "where id = '" + trade.OtherSID + "'";
Reader.Close();
command.ExecuteNonQuery();
}
else
{
command.CommandText = "INSERT INTO test (id, tickets, level) Values ('"+trade.OtherSID+"', '"+ScrapPutUp+"', '0')";
Reader.Close();
command.ExecuteNonQuery();
}
connection.Close();
Can you say me now, how i can fiy the Error?
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 29, 2012, 11:36 AM EDT
1. Do you have the Username and the Ticket information in the Test Table???
2. What exactly do you wanna do?
3. If you have written this code, then where are you getting the issue?
meinenr97 reputationPosted Sep 29, 2012, 11:41 AM EDT
Line: TicketValue = Convert.ToInt32(dr[0].ToString());
-> The name 'dr' does not exist in the ccurrent context
The script should be do:
If a user get 2 tickets:
If the user exist{
add 2 tickets to his account
}
ELSE
{
Create a account and set his tickets to 0
}
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 29, 2012, 11:49 AM EDT
meinenr97 reputationPosted Sep 29, 2012, 11:54 AM EDT
Variablesfor Column's:
id = trade.OtherSID
tickets = ScrapPutUp
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 29, 2012, 11:56 AM EDT
meinenr97 reputationPosted Sep 29, 2012, 12:05 PM EDT
I haven't look, how i have named it in my table ;)
The username column is the id column.
Sorry I have named it column 'id' now...
meinenr97 reputationPosted Sep 29, 2012, 3:45 PM EDT
how to get the Value of the column 'tickets' where the value of the column 'id = XYZ'
id
tickets
XYZ
?
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 30, 2012, 12:51 AM EDT
1. What exactly do you wanna do???
2. How many tables do you have in your database???
3. How many columns are there in the table where you are storing the Tickets???
Elaborate your requirements and the answers for the above mentioned questions correctly and properly.... I am really confused with what you are trying to do
meinenr97 reputationPosted Sep 30, 2012, 1:11 AM EDT
id
tickets
STEAM:123
6
STEAM:915
2
A user get a ticket:
If the user exist in the column 'id', add the ticket in the column 'tickets'.
If the user not exist, cteate a new line with the 'id' from the User and 1 ticket.
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 30, 2012, 1:19 AM EDT
"If the user not exist, cteate a new line with the 'id' from the User and 1 ticket."
So Where are you creating the ID from??? Are you generating it manually, or passing it from another table???
Now for example.... If I do not have a User in my table(he is a new User), then from where are you adding the ID for that user???
Also tell me the name of you table and the connection string you are using for your app....
meinenr97 reputationPosted Sep 30, 2012, 1:30 AM EDT
THE FULL SCRIPT WITH THE VARIABLES:
Variable for 'id' = trade.OtherSID
Variable for 'Tickets' = ScrapPutUp
Id the user 'trade.OtherSID' dosen't exist in the column 'id', add the User and set his 'tickets' to 'ScrapPutUp'.
Else if the user 'trade.OtherSID' exist in the column 'id', add 'ScrapPutUp' tickets to his account.
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 30, 2012, 2:06 AM EDT
meinenr97 reputationPosted Sep 30, 2012, 4:38 AM EDT
command.CommandText = "Update test Set tickets = " Tickets + "where ID = '" + trade.OtherSID + "'";
INTO
command.CommandText = "Update test Set tickets = " + Tickets + "where ID = '" + trade.OtherSID + "'";
AND
command.CommandText = "Insert into test(id,tickets) Values('" + trade.OtherSID + "'", "'" + ScrapPutUp + "'")";
INTO
command.CommandText = "Insert into test(id,tickets) Values('"+trade.OtherSID+"', '"+ScrapPutUp+"')";
But i can't fix the last ERROR:
Tickets = Reader[0].ToString();
-> An implicit conversion of type "string" to "int" is not possible.
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 30, 2012, 4:43 AM EDT
Tickets = Convert.ToInt32(Reader[0].ToString());
meinenr97 reputationPosted Sep 30, 2012, 4:50 AM EDT
ERROR:
Invaild attemp to acces a field before calling Read()
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 30, 2012, 4:54 AM EDT
meinenr97 reputationPosted Sep 30, 2012, 4:58 AM EDT
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 30, 2012, 5:10 AM EDT
Does it display's the row values???
meinenr97 reputationPosted Sep 30, 2012, 5:15 AM EDT
Like this:
Steam:123,1
Steam:856,5
Steam:652,2
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 30, 2012, 5:18 AM EDT
meinenr97 reputationPosted Sep 30, 2012, 5:19 AM EDT
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 30, 2012, 5:29 AM EDT
Copy the Bold script in your code and try running your application!!! Do not copy the entire code... Only copy the bold portion in your code....
string myConnectionString = "SERVER=localhost;" + "DATABASE=test;" + "UID=root;" + "PASSWORD=;";
Copy the Bold script in your code and try running your application!!!
meinenr97 reputationPosted Sep 30, 2012, 5:36 AM EDT
without any ConnectionString etc. ??
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 30, 2012, 5:39 AM EDT
Anyways.... do one thing... Copy paste the entire code I have recently written and make changes wherever you have to.
meinenr97 reputationPosted Sep 30, 2012, 5:42 AM EDT
string myConnectionString = "SERVER=localhost;" + "DATABASE=test;" + "UID=root;" + "PASSWORD=;";
MySqlConnection connection = new MySqlConnection(myConnectionString);
MySqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT * FROM test";
MySqlDataReader Reader;
connection.Open();
Reader = command.ExecuteReader();
int Tickets = 0;
if(Reader.HasRows)
{
while(Reader.Read())
{
Tickets = Convert.toInt32(Reader[0].ToString());
}
Reader.Close();
Tickets = Tickets + ScrapPutUp;
command.CommandText = "Update test Set tickets = " + Tickets + "where ID = '" + trade.OtherSID + "'";
command.ExecuteNonQuery();
}
else
{
command.CommandText = "Insert into test(id,tickets) Values('"+trade.OtherSID+"', '"+ScrapPutUp+"')";
Reader.Close();
command.ExecuteNonQuery();
}
connection.Close();
Than he has an Error in line:
Tickets = Convert.toInt32(Reader[0].ToString());
'System.Convert' does not cantain a definition for 'toInt32'
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 30, 2012, 5:43 AM EDT
meinenr97 reputationPosted Sep 30, 2012, 5:46 AM EDT
ERROR: The input character string has the wrong format.
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 30, 2012, 5:46 AM EDT
Tickets = Int32.Parse(Reader[0].ToString());
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 30, 2012, 5:47 AM EDT
meinenr97 reputationPosted Sep 30, 2012, 5:48 AM EDT
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 30, 2012, 5:49 AM EDT
meinenr97 reputationPosted Sep 30, 2012, 5:50 AM EDT
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 30, 2012, 5:54 AM EDT
meinenr97 reputationPosted Sep 30, 2012, 6:01 AM EDT
string myConnectionString = "SERVER=localhost;" + "DATABASE=test;" + "UID=root;" + "PASSWORD=;";
MySqlConnection connection = new MySqlConnection(myConnectionString);
MySqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT * FROM test";
MySqlDataReader Reader;
connection.Open();
Reader = command.ExecuteReader();
int Tickets = 0;
if(Reader.HasRows)
{
while(Reader.Read())
{
Tickets = Int32.Parse(Reader[0].ToString());
}
Reader.Close();
Tickets = Tickets + ScrapPutUp;
command.CommandText = "Update test Set tickets = " + Tickets + "where ID = '" + trade.OtherSID + "'";
command.ExecuteNonQuery();
}
else
{
command.CommandText = "Insert into test(id,tickets) Values('"+trade.OtherSID+"', '"+ScrapPutUp+"')";
Reader.Close();
command.ExecuteNonQuery();
}
connection.Close();
Here you can see the Errors(sorry, it's in German):
Translate:
German: Die Eingabezeichenfolge hat das falsche Format.
It mean: The input string has the wrong format.
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 30, 2012, 6:05 AM EDT
meinenr97 reputationPosted Sep 30, 2012, 6:11 AM EDT
command.CommandText = "Insert into test(id,tickets) Values('" + trade.OtherSID + "'", "'" + ScrapPutUp + "'")";
but it must be:
command.CommandText = "Insert into test(id,tickets) Values('"+trade.OtherSID+"', '"+ScrapPutUp+"')";
:D
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 30, 2012, 6:13 AM EDT
meinenr97 reputationPosted Sep 30, 2012, 6:14 AM EDT
The Type of the Column 'id' in my database is 'text'
The Type of the Column 'tickets' in my database is 'int'
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 30, 2012, 6:17 AM EDT
meinenr97 reputationPosted Sep 30, 2012, 6:20 AM EDT
No!?!
It doesen't work earlier and it doesen't work now.
If your id dosen't exist, it create a new row,
but if your id already exist, it get an error.
Dosen't work with Varchar(500) too.
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 30, 2012, 6:24 AM EDT
meinenr97 reputationPosted Sep 30, 2012, 6:24 AM EDT
MySqlConnection connection = new MySqlConnection(myConnectionString);
MySqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT * FROM test";
MySqlDataReader Reader;
connection.Open();
Reader = command.ExecuteReader();
while (Reader.Read())
{
string row = "";
for (int i = 0; i < Reader.FieldCount; i++)
row += Reader.GetValue(i).ToString() + ", ";
Console.WriteLine(row);
}
Reader.Close();
command.CommandText = "Select tickets from test where ID = '" + trade.OtherSID + "'";
Reader = command.ExecuteReader();
int Tickets = 0;
if(Reader.HasRows)
{
while(Reader.Read())
{
Tickets = Int32.Parse(Reader[0].ToString());
}
Reader.Close();
Tickets = Tickets + ScrapPutUp;
command.CommandText = "Update test Set tickets = " + Tickets + "where ID = '" + trade.OtherSID + "'";
command.ExecuteNonQuery();
}
else
{
command.CommandText = "Insert into test(id,tickets) Values('"+trade.OtherSID+"', '"+ScrapPutUp+"')";
Reader.Close();
command.ExecuteNonQuery();
}
connection.Close();
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 30, 2012, 7:35 AM EDT
Sukesh MarlaRank #461 · 5.4K reputationPosted Sep 30, 2012, 10:16 AM EDT
Do just one thing, Upload the source code and Database script(fo table), I will check and revert back
meinenr97 reputationPosted Sep 30, 2012, 10:23 AM EDT
meinenr97 reputationPosted Sep 30, 2012, 11:39 AM EDT
Thanks for your help!
string myConnectionString = "SERVER=localhost;" + "DATABASE=test;" + "UID=root;" + "PASSWORD=;";
MySqlConnection connection = new MySqlConnection(myConnectionString);
MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;
connection.Open();
command.CommandText = "Select tickets from test where id = '" + trade.OtherSID + "'";
Reader = command.ExecuteReader();
int Tickets = 0;
if(Reader.HasRows)
{
while(Reader.Read())
{
string row = "";
for (int i = 0; i < Reader.FieldCount; i++)
row += Reader.GetValue(i).ToString() + "";
Console.WriteLine(row);
trade.SendMessage("You already have: " + row + " Tickets." );
Tickets = Convert.ToInt32(row) + ScrapPutUp;
}
trade.SendMessage("Now you have: " + Tickets + " Tickets." );
//command.CommandText = "Update test Set tickets = " + Tickets + "where ID = '" + trade.OtherSID + "'";
command.CommandText = "Update test Set tickets = '" + Tickets + "'" + "where ID = '" + trade.OtherSID + "'";
Reader.Close();
command.ExecuteNonQuery();
}
else
{
command.CommandText = "Insert into test(id,tickets) Values('"+trade.OtherSID+"', '"+ScrapPutUp+"')";
Reader.Close();
command.ExecuteNonQuery();
}
connection.Close();