LCKB
[C#] Switch Method - Printable Version

+- LCKB (https://lckb.dev/forum)
+-- Forum: ** OLD LCKB DATABASE ** (https://lckb.dev/forum/forumdisplay.php?fid=109)
+--- Forum: Programmers Gateway (https://lckb.dev/forum/forumdisplay.php?fid=196)
+---- Forum: Coders Talk (https://lckb.dev/forum/forumdisplay.php?fid=192)
+---- Thread: [C#] Switch Method (/showthread.php?tid=1155)



- Nikolee - 09-25-2012


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 ?




- Wizatek - 09-25-2012



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));




- HateMe - 09-25-2012


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;

}




- Nikolee - 09-26-2012

Ah guys you are the best :=)