0

i am using a combobox including Name Company and Country so the user could choose from them to specify what form does he want to search in a textbox and i need to view my search results in a different form (results.cs) and my search engine is on (main.cs) how could i do it ?

private void button1_Click(object sender, EventArgs e)
{
  this.Hide();

  if (comboBox1.Text == "Name")
  {
      String var;
      SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Seif-\Documents\Visual Studio 2013\Projects\BusinessCard\BusinessCard\BusinessCards.mdf;Integrated Security=True");
      SqlCommand sc = new SqlCommand("SELECT Name, Post, Company, Country, Email, Mobile, Tel1, Tel2, Fax, Address FROM BC where Name LIKE '" + textBox1.Text + "'", conn);
      SqlDataAdapter sda = new SqlDataAdapter(sc);
      DataTable dt = new DataTable();
      sda.Fill(dt);
      var = (string)sc.ExecuteScalar();
      Search f2 = new Search();
      f2.Show();

  }
  else if (comboBox1.Text == "Company")
  {
    String var;
    SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Seif-\Documents\Visual Studio 2013\Projects\BusinessCard\BusinessCard\BusinessCards.mdf;Integrated Security=True");
    SqlCommand sc = new SqlCommand("SELECT Name, Post, Company, Country, Email, Mobile, Tel1, Tel2, Fax, Address FROM BC where Company LIKE '" + textBox1.Text + "'", conn);
    SqlDataAdapter sda = new SqlDataAdapter(sc);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    var = (string)sc.ExecuteScalar();
    Search f2 = new Search();
    f2.Show();
  }
  else if (comboBox1.Text == "Country")
  {
    String var;
    SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Seif-\Documents\Visual Studio 2013\Projects\BusinessCard\BusinessCard\BusinessCards.mdf;Integrated Security=True");
    SqlCommand sc = new SqlCommand("SELECT Name, Post, Company, Country, Email, Mobile, Tel1, Tel2, Fax, Address FROM BC where Country LIKE '" + textBox1.Text + "'", conn);
    SqlDataAdapter sda = new SqlDataAdapter(sc);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    var = (string)sc.ExecuteScalar();
    Search f2 = new Search();
    f2.Show();

  }
}
maxhb
  • 8,554
  • 9
  • 29
  • 53

4 Answers4

1

You can do this by any one of following two ways

  1. Create public property and assign the values to that.
  2. Pass the value through constructor and set them in your search form

Code:

Search f2 = new Search(); 
f2.result = <<search result variable>>
f2.Show();

Search f2 = new Search(<<search result variable>>); 
f2.Show();
Hambone
  • 15,600
  • 8
  • 46
  • 69
Sathik Khan
  • 439
  • 4
  • 13
  • For the record, I didn't mean to poach your answer... this appears to be the same thing I said on the core of the question, although I had some unrelated commentary. If the OP likes the answer that Sathik and I came up with, please mark his as correct. – Hambone Feb 21 '16 at 02:47
1

You need to cal the 2nd form by an instance of the class. See my 2 form project Form 1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        Form2 form2;
        public Form1()
        {
            InitializeComponent();
            form2 = new Form2(this);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            form2.Show();
            string results = form2.GetData();
        }
    }
}

Form 2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form2 : Form
    {
        Form1 form1;
        public Form2(Form1 nform1)
        {
            InitializeComponent();

            this.FormClosing += new FormClosingEventHandler(Form2_FormClosing);
            form1 = nform1;
            form1.Hide();
        }
        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            //stops for from closing
            e.Cancel = true;
            this.Hide();
        }
        public string GetData()
        {
            return "The quick brown fox jumped over the lazy dog";
        }

    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
1

The most common way to do this is pass the DataTable in the Search constructor.

Search f2 = new Search(dt);

And in the Search form you would have an private member to hold the value.

private DataTable _results;
public Search(DataTable table)
{
   _results = table;
}

This way you can use _results everywhere in Search

When using SQL in your applications you should not concat the values in a string to avoid SqlInjection. There is a class SqlParameter for that and you can refer to this question for the right way to use it.

Here are a modified version of your code using SqlParameter and closing the SqlConnection

string command = string.Format(@"SELECT Name, Post, Company, Country, Email, Mobile, Tel1, Tel2, Fax, Address FROM BC where {0} LIKE @value", combobox1.Text); 
DataTable dt;

using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Seif-\Documents\Visual Studio 2013\Projects\BusinessCard\BusinessCard\BusinessCards.mdf;Integrated Security=True"))
{
    SqlCommand sc = new SqlCommand(command, conn);
    sc.Parameters.Add("@value", textBox1.Text);
    SqlDataAdapter sda = new SqlDataAdapter(sc);
    dt = new DataTable();
    sda.Fill(dt);
}

Search f2 = new Search(dt);
f2.Show();
Community
  • 1
  • 1
vappolinario
  • 608
  • 1
  • 9
  • 22
0

On the Results.cs class, change the constructor to include the data:

public class Results
{
    private DataTable _ResultsTable;

    public Results(DataTable ResultsTable)
    {
        _ResultsTable;
    }
}

Which means the form instantiation would be:

Results resForm = new Results(dt);   

This presupposes you would never load the Results form without a dataset.

Or, if you don't want to force it to be declared up front, you can always just make it a property within results.cs:

public DataTable ResultsTable { get; set; }

And then you can access it like you would any other property:

Results resForm = new Results();
// various lines of code from your example above
resForm.ResultTable = dt;

For what it's worth, I think you have more code than you need in your queries from the database. I believe much of your button1_Click code can be replaced with something like this:

private void button1_Click(object sender, EventArgs e)
{
    this.Hide();

    SqlConnection conn = new SqlConnection(@"<your connection string>");
    SqlCommand sc = new SqlCommand(string.Format(@"
        SELECT Name, Post, Company, Country, Email, Mobile, Tel1, Tel2, Fax, Address
        FROM BC
        where {0} like @VAL", comboBox1.Text), conn);

    sc.Parameters.AddWithValue("@VAL", textBox1.Text);

    SqlDataAdapter sda = new SqlDataAdapter(sc);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    var = (string)sc.ExecuteScalar();
    Search f2 = new Search();
    f2.Show();
}

The use of a parameter will prevent not only SQL Injection, but it will also handle any odd text in the textBox1, such as if the user enters:

I think I'll have some cake

Which would blow up your code because of the apostrophe.

It's also more scalable, were you to add more search options or apply this to future tables.

Hambone
  • 15,600
  • 8
  • 46
  • 69