Posts: 528
Threads: 50
Thanks Received:
0 in 0 posts
Thanks Given: 0
Joined: Oct 2011
Reputation:
0
Hey, im Trying to use my MySQL Connection string, with Strings so without that i need to write" host = "127.0.0.1" "
but there are a little Problem i hope some of you can find it^^
The Error: The connection property has not been set or is null.
// Make the Strings
public partial class user_index : Form
{
public string host;
public string user;
public string db;
public string pw;
public user_index()
{
InitializeComponent();
Initialize();
}
//Initialize the Strings (and define)
private void Initialize()
{
Form1 f1 = new Form1();
host = f1.textBox1.Text;
user = f1.textBox3.Text;
db = f1.textBox5.Text;
pw = f1.textBox4.Text;
}
//Insert
public void Insert()
{
MySqlConnection con;
MySqlConnectionStringBuilder csb = new MySqlConnectionStringBuilder();
csb.Server = host;
csb.UserID = user;
csb.Database = db;
csb.Password = pw;
con = new MySqlConnection(csb.ConnectionString);
MySqlConnection connection = new MySqlConnection(csb.ConnectionString);
DataTable datatable = new DataTable();
String sql1 = "SELECT * FROM t_characters user_code WHERE a_nick =" + textBox1.Text;
MySqlDataAdapter dataAdapter = new MySqlDataAdapter(sql1, connection);
dataAdapter.Fill(datatable);
foreach (DataRow row in datatable.Rows)
{
ListViewItem item = new ListViewItem(
new string[] {
row["a_user_index"].ToString()});
listView1.Items.Add(item);
}
}
Posts: 768
Threads: 40
Thanks Received:
0 in 0 posts
Thanks Given: 0
Joined: May 2011
Reputation:
0
On what line u get that error?
MySqlDataAdapter dataAdapter = new MySqlDataAdapter(sql1, connection);
probably (not sure) u need to add connection.Open(); before that line
Posts: 335
Threads: 22
Thanks Received:
0 in 0 posts
Thanks Given: 0
Joined: Aug 2011
Reputation:
0
yes i see to the mysql open and close is missing
............................
MySqlConnection connection = new MySqlConnection(csb.ConnectionString);
connection.Open();
DataTable datatable = new DataTable();
String sql1 = "SELECT * FROM t_characters user_code WHERE a_nick =" + textBox1.Text;
MySqlDataAdapter dataAdapter = new MySqlDataAdapter(sql1, connection);
dataAdapter.Fill(datatable);
foreach (DataRow row in datatable.Rows)
{
ListViewItem item = new ListViewItem(
new string[] {
row["a_user_index"].ToString()});
listView1.Items.Add(item);
}
// also dont forget to close it
connection.Close();
Posts: 335
Threads: 22
Thanks Received:
0 in 0 posts
Thanks Given: 0
Joined: Aug 2011
Reputation:
0
an other way to get to get stuff out the datatabel
public static string host, password, user;
public static string database_db;
.............................
public static List<CharacterContainer> Get_CharacterList(int UserID)
{
List<CharacterContainer> CharacterList = new List<CharacterContainer>();
try
{
MySqlConnection mysqlCon = new MySqlConnection(strProvider_db);
mysqlCon.Open();
MySqlCommand Command = new MySqlCommand("SELECT * FROM t_characters WHERE a_user_index = " + UserID + ";", mysqlCon);
Command.ExecuteNonQuery();
MySqlDataReader Reader = Command.ExecuteReader();
while (Reader.Read())
{
CharacterContainer temp = new CharacterContainer();
temp.a_index = Convert.ToInt32(Reader["a_index"]);
temp.a_user_index = Convert.ToInt32(Reader["a_user_index"]);
temp.a_name = (string)Reader["a_name"];
temp.a_nick = (string)Reader["a_nick"];
temp.a_enable = Convert.ToInt32(Reader["a_enable"]);
CharacterList.Add(temp);
}
mysqlCon.Close();
}
catch (Exception ex)
{
LogScreen.Error(ex.ToString());
}
return CharacterList;
}
...............................
// need an other class
public class CharacterContainer
{
public int a_index { get; set; }
public int a_user_index { get; set; }
public string a_name { get; set; }
public string a_nick { get; set; }
public int a_enable { get; set; }
}
................................
// how to use it
int UserID = 2032; // 2032 is the user Demo from ur starter packer
List<CharacterContainer> Characters = mySQL.Get_CharacterList(UserID);
for (int c = 0; c < Characters.Count; c++)
{
lock (_lock3)
{
int CharID = Characters[c].a_index;
string CharName = Characters[c].a_nick;
if (Characters[c].a_enable != 0)
{
Console.writeline("CharID: " +CharID + " UserName: " + UserName + " CharName:" + CharName);
}
}
}
Posts: 528
Threads: 50
Thanks Received:
0 in 0 posts
Thanks Given: 0
Joined: Oct 2011
Reputation:
0
Forgot all this i got a Fix but not really Logical..
Look this Class i marked where you should read ^^:
class Force
{
public Force()
{
Initialize();
}
public string Server;
private string UserID;
private string DB;
private string pw;
public void Initialize()
{
Form1 f1 = new Form1();
f1.Show(); // Here is the Fail, if i let it Open i can connect but if i dont include this it tells me the Error..
Server = f1.textBox1.Text;
UserID = f1.textBox3.Text;
DB = f1.textBox5.Text;
pw = f1.textBox4.Text;
}
public void MySql()
{
MySqlConnection con;
MySqlConnectionStringBuilder csb = new MySqlConnectionStringBuilder();
csb.Server = Server;
csb.UserID = UserID;
csb.Database =DB;
csb.Password = pw;
con = new MySqlConnection(csb.ConnectionString);
MySqlConnection connection = new MySqlConnection(csb.ConnectionString);
connection.Open();
}
}