Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a Windows Form Application. I have a function which uses Linq to SQL to find the records I want but I don't know how to call this function in my button.click event so that the click function will launch my other function.

Also, in my button.click event I will need to set the textBox1.Text value to the sql statement but how would that be done?

C#
namespace InsertParameter
{
public partial class Form1 : Form
{
    LinqtoSqlDataContext linqStud = new LinqtoSqlDataContext();

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // how do I call the below function here???
    }

    public Exception searchCity(Student student, string searchCity)
    {
        try
        {
            if (string.IsNullOrEmpty(searchCity))
            {
                MessageBox.Show("Please enter a value");
            }
            else
            {
                var City = from stud in linqStud.Students
                           where stud.City == searchCity
                           select stud;

                dataGridView1.DataSource = City;

            }
            return null;
        }
        catch (Exception ex)
        {
            return ex;
        }
    }
}
}


Thanks

Nick
Posted
Comments
[no name] 4-Jul-13 11:21am    
private void button1_Click(object sender, EventArgs e)
{
searchCity(instanceOfStudent, "some city");
}
JeremH 4-Jul-13 11:27am    
Hi,

can you set a breakpoint in your clickevent to see that your event is ok. After you can call all method.

It depends on how you are going to get the student parameter - but since your method doesn't actually use it...for the moment, you can just use a null:
C#
private void button1_Click(object sender, EventArgs e)
{
    Exception ex = searchCity(null, "New York");
    if (ex != null)
    {
        throw(ex);
    }
}
 
Share this answer
 
update - here is the finished solution:
C#
public partial class Form1 : Form
    {
        LinqtoSqlDataContext linqStud = new LinqtoSqlDataContext();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Student student = new Student();
            string city = textBox1.Text;

            searchCity(student, city);
        }

        public Exception searchCity(Student student, string searchCity)
        {
            try
            {
                if (string.IsNullOrEmpty(searchCity))
                {
                    MessageBox.Show("Please enter a value");
                }
                else
                {
                    var City = from stud in linqStud.Students
                               where stud.City == searchCity
                               select stud;

                    dataGridView1.DataSource = City;

                }
                return null;
            }
            catch (Exception ex)
            {
                return ex;
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900