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 ArurPosted Sep 30, 2012, 2:28 PM
"Select tickets from test where id = '" + trade.OtherSID + "'";
meinenrPosted Sep 30, 2012, 3:39 PM
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();
meinenrPosted Sep 30, 2012, 2:23 PM
Sukesh MarlaPosted Sep 30, 2012, 2:16 PM
Do just one thing, Upload the source code and Database script(fo table), I will check and revert back
Shivanand ArurPosted Sep 30, 2012, 11:35 AM
meinenrPosted Sep 30, 2012, 10:24 AM
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 ArurPosted Sep 30, 2012, 10:24 AM
meinenrPosted Sep 30, 2012, 10:20 AM
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 ArurPosted Sep 30, 2012, 10:17 AM
meinenrPosted Sep 30, 2012, 10:14 AM
The Type of the Column 'id' in my database is 'text'
The Type of the Column 'tickets' in my database is 'int'
Shivanand ArurPosted Sep 30, 2012, 10:13 AM
meinenrPosted Sep 30, 2012, 10:11 AM
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 ArurPosted Sep 30, 2012, 10:05 AM
meinenrPosted Sep 30, 2012, 10:01 AM
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 ArurPosted Sep 30, 2012, 9:54 AM
meinenrPosted Sep 30, 2012, 9:50 AM
Shivanand ArurPosted Sep 30, 2012, 9:49 AM
meinenrPosted Sep 30, 2012, 9:48 AM
Shivanand ArurPosted Sep 30, 2012, 9:47 AM
Shivanand ArurPosted Sep 30, 2012, 9:46 AM
Tickets = Int32.Parse(Reader[0].ToString());
meinenrPosted Sep 30, 2012, 9:46 AM
ERROR: The input character string has the wrong format.
Shivanand ArurPosted Sep 30, 2012, 9:43 AM
meinenrPosted Sep 30, 2012, 9:42 AM
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 ArurPosted Sep 30, 2012, 9:39 AM
Anyways.... do one thing... Copy paste the entire code I have recently written and make changes wherever you have to.
meinenrPosted Sep 30, 2012, 9:36 AM
without any ConnectionString etc. ??
Shivanand ArurPosted Sep 30, 2012, 9:29 AM
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!!!
meinenrPosted Sep 30, 2012, 9:19 AM
Shivanand ArurPosted Sep 30, 2012, 9:18 AM
meinenrPosted Sep 30, 2012, 9:15 AM
Like this:
Steam:123,1
Steam:856,5
Steam:652,2
Shivanand ArurPosted Sep 30, 2012, 9:10 AM
Does it display's the row values???
meinenrPosted Sep 30, 2012, 8:58 AM
Shivanand ArurPosted Sep 30, 2012, 8:54 AM
meinenrPosted Sep 30, 2012, 8:50 AM
ERROR:
Invaild attemp to acces a field before calling Read()
Shivanand ArurPosted Sep 30, 2012, 8:43 AM
Tickets = Convert.ToInt32(Reader[0].ToString());
meinenrPosted Sep 30, 2012, 8:38 AM
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 ArurPosted Sep 30, 2012, 6:06 AM
meinenrPosted Sep 30, 2012, 5:30 AM
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 ArurPosted Sep 30, 2012, 5:19 AM
"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....
meinenrPosted Sep 30, 2012, 5:11 AM
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 ArurPosted Sep 30, 2012, 4:51 AM
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
meinenrPosted Sep 29, 2012, 7:45 PM
how to get the Value of the column 'tickets' where the value of the column 'id = XYZ'
id
tickets
XYZ
?
meinenrPosted Sep 29, 2012, 4:05 PM
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...
Shivanand ArurPosted Sep 29, 2012, 3:56 PM
meinenrPosted Sep 29, 2012, 3:54 PM
Variablesfor Column's:
id = trade.OtherSID
tickets = ScrapPutUp
Shivanand ArurPosted Sep 29, 2012, 3:49 PM
meinenrPosted Sep 29, 2012, 3:41 PM
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 ArurPosted Sep 29, 2012, 3:36 PM
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?
meinenrPosted Sep 29, 2012, 3:30 PM
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 ArurPosted Sep 29, 2012, 3:24 PM
meinenrPosted Sep 29, 2012, 3:21 PM
Line: TicketValue = Convert.ToInt32(dr[0].ToString());
-> The name 'dr' does not exist in the ccurrent context
I have make something wrong?
Shivanand ArurPosted Sep 29, 2012, 3:05 PM
meinenrPosted Sep 29, 2012, 3:00 PM
Shivanand ArurPosted Sep 29, 2012, 2:53 PM
meinenrPosted Sep 29, 2012, 2:51 PM
But I am able to change it, if you want.
Shivanand ArurPosted Sep 29, 2012, 2:49 PM
meinenrPosted Sep 29, 2012, 2:46 PM
Shivanand ArurPosted Sep 29, 2012, 2:43 PM