3 Replies - 3067 Views - Last Post: 19 March 2009 - 02:51 AM Rate Topic: -----

#1 Premier2k  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 178
  • Joined: 26-August 08

Passing SQL data between forms

Post icon  Posted 17 March 2009 - 01:46 PM

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?
 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=******;PASSWORD=***********;");
					//Use the connection string to open a connection to the database
					conn.Open();					
					MySqlCommand dataCommand = new MySqlCommand(strSQL, conn);
					//Run the query
					MySqlDataReader dataReader = dataCommand.ExecuteReader();


					while (dataReader.Read())
					{
						searchresult result = new searchresult();
						result.Show();
						this.Close();
					}
				}

				catch (Exception err)
				{
					MessageBox.Show(err.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
				}
			}
		}


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;

tbFirst.Text = thisstoredcolumn1
tbSecond.Text = thisstoredcolumn2
tbNick.Text = thisstoredcolumn3


Premier2k

Is This A Good Question/Topic? 0
  • +

Replies To: Passing SQL data between forms

#2 newProgram  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 160
  • Joined: 28-October 08

Re: Passing SQL data between forms

Posted 17 March 2009 - 07:43 PM

View PostPremier2k, on 17 Mar, 2009 - 12:46 PM, said:

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?
 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=******;PASSWORD=***********;");
					//Use the connection string to open a connection to the database
					conn.Open();					
					MySqlCommand dataCommand = new MySqlCommand(strSQL, conn);
					//Run the query
					MySqlDataReader dataReader = dataCommand.ExecuteReader();


					while (dataReader.Read())
					{
						searchresult result = new searchresult();
						result.Show();
						this.Close();
					}
				}

				catch (Exception err)
				{
					MessageBox.Show(err.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
				}
			}
		}


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;

tbFirst.Text = thisstoredcolumn1
tbSecond.Text = thisstoredcolumn2
tbNick.Text = thisstoredcolumn3


Premier2k

Here are some example in passing data between forms
//1.) Goto Form2 then Double click it. At the code type this.

public Form2(string sTEXT)
{
InitializeComponent();
textBox1.Text = sTEXT;
}

//2.) Goto Form1 then Double click it. At the code type this.
//At your command button in Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 sForm = new Form2(textBox1.Text);
sForm.Show();
}


It also answers your second question.

This post has been edited by newProgram: 17 March 2009 - 07:46 PM

Was This Post Helpful? 0
  • +
  • -

#3 Ryan Marfone  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 87
  • Joined: 23-February 09

Re: Passing SQL data between forms

Posted 17 March 2009 - 09:29 PM

Ideally you should seperate your UI from your data access layer. This is, in general, a better design.
Was This Post Helpful? 0
  • +
  • -

#4 Premier2k  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 178
  • Joined: 26-August 08

Re: Passing SQL data between forms

Posted 19 March 2009 - 02:51 AM

OK, I have modified the way I do things and the information returned is now displayed on the same page, I couldn't see a valid reason for displaying on a separate form. I still struggling to get the dataset and data adapter to work though. I have a new method in my library which is this one:

public static DataTable _f_filldTP(string parmSQL, string parmTable, MySqlParameter[] parmParms, MySqlConnection parmConn)
		{
			try
			{
				//Connect to database
				MySqlCommand selectCMD = new MySqlCommand(parmSQL, parmConn);
				selectCMD.CommandType = CommandType.Text;

				// Create Parameters 
				for (int j = 0; j < parmParms.Length; j++) 
					{ 
					selectCMD.Parameters.Add(parmParms[j]); 
					}

				// Program the adapter with the command 
				MySqlDataAdapter selectDA = new MySqlDataAdapter(); 
				selectDA.SelectCommand = selectCMD; 

				// No need to Open, fill takes care of this 
				// Execute command 
				DataTable selectDT = new DataTable(parmTable); 
				selectDA.Fill(selectDT); 
				selectCMD.Dispose(); 
				selectDA.Dispose(); 

				//Check the connection status and if it's open then close it.
				if (parmConn.State.Equals(ConnectionState.Open)) 
					{ 
					parmConn.Close(); 
					} 

				// Terminate connection and return the DataSet 
				return selectDT; 
			 } 
			catch
			{
				//Error Handling here
			}


this takes the args
(string parmSQL, string parmTable, MySqlParameter[] parmParms, MySqlConnection parmConn)


I have most of my args passed through as shown in my button 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 + "'";
					string strSQL = "SELECT nickName FROM playernames WHERE nickName = "+"'search'";
					MySqlConnection conn = new MySqlConnection("SERVER = localhost" + ";DATABASE=pok3r;UID=root;PASSWORD=Password12;");
					MySqlParameter test;
					library dt = new library();
					library._f_filldTP(strSQL, "getPlayer", ????? ,conn);


But what is MySQL Parameters? What replaces the ???. I have my connection string, I have my MYSQL query and I have the table name. What are MySQL Parameters? It looks like it's an array?
I'm getting closer with this but I'm still not quite there yet.

Premier2k
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1