[C#] Switch Method
#1

Hey im back with spam with my questions

Im working just 4 fun on a t_magic tool and i have one problem with the Swtich(variable) method (or mysql?)

 

I have this code :

 

if (listBox1.SelectedIndex != -1)
{
string Index = listBox1.SelectedItem.ToString();
textBox2.Text = Index;
}

MySqlConnection con;
MySqlConnectionStringBuilder csb = new MySqlConnectionStringBuilder();
csb.Server = Server;
csb.UserID = user;
csb.Database = db;
csb.Password = pw;
con = new MySqlConnection(csb.ConnectionString);
MySqlConnection connection = new MySqlConnection(csb.ConnectionString);
connection.Open();

string _Index = listBox1.SelectedItem.ToString();

string _Sql1 = @"SELECT * FROM t_magic a_index WHERE a_name ="+"'"+ _Index+"'";
DataTable dttable = new DataTable();
MySqlCommand insertCommand = new MySqlCommand(_Sql1, connection);
int i = insertCommand.ExecuteNonQuery();
MySqlDataAdapter dtAdp = new MySqlDataAdapter(_Sql1, connection);
dtAdp.Fill(dttable);
string result = Convert.ToString(insertCommand.ExecuteScalar());

string _sql2 = @"SELECT * FROM t_magic a_damagetype WHERE a_name =" + "'" + _Index + "'"; //problem is here
DataTable dtttable = new DataTable();
MySqlCommand insertCommand2 = new MySqlCommand(_sql2, connection);
int g = insertCommand2.ExecuteNonQuery();
MySqlDataAdapter dtAdp1 = new MySqlDataAdapter(_sql2, connection);
dtAdp.Fill(dttable);
string result1 = Convert.ToString(insertCommand2.ExecuteScalar());
MessageBox.Show(result1); // and here : it sows me the Index from the Skill and not the Damagetyp!
switch (result1)
{
case "0":
DamageTyp.Text = "No Damage";
break;

case "1":
DamageTyp.Text = "Normal Damage";
break;

case "2":
DamageTyp.Text = "Damage in %";
break;

case "60": // 60 is the Index of a skill! i cheked if i fail!
DamageTyp.Text = "You Fail!";
break;

 

So my Problem is it shows me the Index from the Skill and not the Damagetyp, where is the fail ?

#2


string _sql2 = @"SELECT * FROM t_magic a_damagetype WHERE a_name =" + "'" + _Index + "'";
string result1 = Convert.ToString(insertCommand2.ExecuteScalar());

 

25.2.3.1.9. ExecuteScalar

 

Executes the query, and returns the first column of the first row in the result set returned by the query. Extra columns or rows are ignored.

 

Returns: The first column of the first row in the result set, or a null reference if the result set is empty

2

 

 

The problem is because u do select * from t_magic, and the first column is a_index

 

 

MySqlCommand cmd = new MySqlCommand();
cmd.Connection = con;

cmd.CommandText = "SELECT a_damagetype FROM t_magic WHERE a_name = @name";
cmd.Prepare();

cmd.Parameters.AddWithValue("@name", _Index);

object res = cmd.ExecuteScalar();

if( res != null )
{
int damageType = Convert.ToInt32(res);

switch(damageType)
{
case 0:
DamageTyp.Text = "No Damage";
break;

case 1:
DamageTyp.Text = "Normal Damage";
break;

case 2:
DamageTyp.Text = "Damage in %";
break;

case 60: // 60 is the Index of a skill! i cheked if i fail!
DamageTyp.Text = "You Fail!";
break;

}
}
else
MessageBox.Show(string.Format("Not found magic : {0}", _Index));

#3

your mysql command is worng?

 

string _sql2 = "SELECT a_damagetype FROM t_magic WHERE a_name =\"" + _Index + "\";";

switch (result1)
{
case "0":
DamageTyp.Text = "No Damage";
break;

case "1":
DamageTyp.Text = "Normal Damage";
break;

case "2":
DamageTyp.Text = "Damage in %";
break;

default:
DamageTyp.Text = "You Fail!";
break;

}

#4
Ah guys you are the best :=)



Forum Jump:


Users browsing this thread: 1 Guest(s)