-
March 17th, 2009, 05:25 PM
#1
Searching MySQL data and displaying in another form
Hi all,
I am having some trouble displaying some data I have pulled from a MySQL database. I have a text field and a search button, the user enters a name into the text field and presses the search button. The form should then close and another form opens with 3 text fields which are populated with the information from the database.
I can get the information, but I'm not sure how to pass the data over to another form; here's what I have so far.
This is my code for the search button, so after the user presses it, it stores the data it retrieves.......I think......
Firstly, what is it storing the data as? A variable? What is it? How do I call it?
Code:
private void btnSearch_Click(object sender, RoutedEventArgs e)
{
//Check the text present in the Nickname text field
if (tbNname.Text == "")
{
//If it's empty then show this message
MessageBox.Show("Please enter player nickname", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
//otherwise
else
{
try
{
//Store the contents of Nickname textbox as a variable called 'search'
string search = "'" + tbNname.Text + "'";
//Store the SQL query as a variable called 'strSQL'
string strSQL = "SELECT nickName FROM playernames WHERE nickName = "+"'search'";
//Create the connection string for the database and store it as 'conn'
MySqlConnection conn = new MySqlConnection("SERVER = localhost" + ";DATABASE=pok3r;UID=root;PASSWORD=Password12;");
//Use the connection string to open a connection to the database
conn.Open();
//Load the query into the DBMS
MySqlCommand dataCommand = new MySqlCommand(strSQL, conn);
//Run the query
MySqlDataReader dataReader = dataCommand.ExecuteReader();
conn.Close();
while (dataReader.Read())
{
searchresult result = new searchresult();
result.Show();
this.Close();
}
}
catch (Exception err)
{
MessageBox.Show("Unable to process request. See 'logfile' for more details", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
string strLogText = err.Message.ToString();
database Log = new database();
Log.WriteToLog(strLogText);
}
}
}
Lastly, assuming in my next form the 3 fields are tbFirst, tbSecond and tbNick. How would I display that information inside the text box?
I'm guessing it would be something along the lines of;
Code:
tbFirst.Text = thisstoredcolumn1
tbSecond.Text = thisstoredcolumn2
tbNick.Text = thisstoredcolumn3
Thanks all,
Premier2k
-
March 18th, 2009, 04:15 AM
#2
Re: Searching MySQL data and displaying in another form
-
March 18th, 2009, 04:25 AM
#3
Re: Searching MySQL data and displaying in another form
Thanks for that! I have absolutely no idea what events and delegates are, or how to implement this into my design. But I have a place to start learning now! {Drags out C# book and starts to read}
Premier2k
-
March 18th, 2009, 11:44 AM
#4
Re: Searching MySQL data and displaying in another form
I've had another think about this and decided to keep the results shown on the same screen. I can't see a valid reason for creating another window to view the results.
So I'm playing around with datasets and adaptors. I'm still stuck with this and can't seem to get it to work.
Here is my form code; (There is more, but it's not relevant)
Code:
public DataSet ds1;
public System.Data.Odbc.OdbcDataAdapter da1;
public MySqlConnection conn = new MySqlConnection("SERVER = localhost" + ";DATABASE=pok3r;UID=root;PASSWORD=Password12;");
private void btnSearch_Click(object sender, RoutedEventArgs e)
{
//Check the text present in the Nickname text field
if (tbNname.Text == "")
{
//If it's empty then show this message
MessageBox.Show("Please enter player nickname", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
//otherwise
else
{
try
{
//Store the contents of Nickname textbox as a variable called 'search'
string search = "'" + tbNname.Text + "'";
//string strSQL = "SELECT nickName FROM playernames WHERE nickName = "+"'search'";
library returnRows = new library();
returnRows.SelectRows(ds1, conn.ConnectionString, "SELECT nickName FROM playernames WHERE nickName = " + "'search'", "searchResult");
DataRow dRow = ds1.Tables["searchResult"].Rows[0];
firstName.Text = dRow.ItemArray.GetValue(1).ToString();
lastName.Text = dRow.ItemArray.GetValue(2).ToString();
and then here is my method within my library that handles the query.
Code:
public DataSet SelectRows(DataSet dataset, string connection, string query, string table)
{
MySqlConnection conn = new MySqlConnection(connection);
MySqlDataAdapter adapter = new MySqlDataAdapter();
adapter.SelectCommand = new MySqlCommand(query, conn);
adapter.Fill(dataset, table);
return dataset;
}
I'm getting an error about returning empty data, I'm missing something and I can't for the life of me work out what it is. Can anyone help?
Premier2k
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|