Click here to Skip to main content
15,886,665 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
suppose i link one form to another, and i want to write an sql query to output the result onto the second form, using a datagrid view, how do i go about doing this?

my code so far is..

C#
Form2 secondform=new Form2();

       private void button3_Click(object sender, EventArgs e)
       {
           secondform.Show();

        }

how do i go about writing the code now?

Addition, full code from comment:

C#
namespace final2
{
    public partial class Form1 : Form
    {
        private SqlConnection con;
        private SqlCommand command;
        private SqlDataAdapter adapter;
        private DataSet dataset;
        public DataGridView dg;

        public Form1()
        {
            InitializeComponent();

            con = new SqlConnection();
            command = con.CreateCommand();
            con.ConnectionString = "Data Source=CASSINI-003-PC;Initial Catalog=studentdb;Integrated Security=True";
            adapter = new SqlDataAdapter(command);
            dataset = new DataSet();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            command.Parameters.Clear();
            command.Parameters.AddWithValue("@StudentID", textBox1.Text);
            command.Parameters.AddWithValue("@Name", textBox2.Text);
            command.Parameters.AddWithValue("@Age", textBox3.Text);
            command.Parameters.AddWithValue("@Gender", textBox4.Text);
            command.Parameters.AddWithValue("@Courseno", listBox1.SelectedItem);
            command.CommandText = "INSERT into details" + "(StudentID,Name,Age,Gender,Courseno)VALUES" + "(@StudentID,@Name,@Age,@Gender,@Courseno)";

            try
            {
                con.Open();

                int result = command.ExecuteNonQuery();

                if (result > 0)
                    MessageBox.Show("student successfully updated");
                else
                    MessageBox.Show("failed to update");
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }

            ClearFields();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            command.Parameters.Clear();
            command.Parameters.AddWithValue("@StudentID", textBox1.Text);
            command.CommandText = "SELECT * FROM details WHERE StudentID=@StudentID";

            dataset.Tables.Clear();

            int result = adapter.Fill(dataset, "details");

            if (result > 0)
            {
                DataRow srow = dataset.Tables["details"].Rows[0];
                textBox1.Text = srow["StudentID"].ToString();
                textBox2.Text = srow["Name"].ToString();
                textBox3.Text = srow["Age"].ToString();
                textBox4.Text = srow["Gender"].ToString();
                listBox1.SelectedItem = srow["Courseno"].ToString();

            }
            else
            {
                MessageBox.Show("Student does not exist");
            }
       
        }

        void ClearFields()
        {
            textBox1.Text = String.Empty;
            textBox2.Text = String.Empty;
            textBox3.Text = String.Empty;
            textBox4.Text = String.Empty;
            

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string currentitem = listBox1.SelectedItem.ToString();
        }

        Form2 secondform=new Form2();

        private void button3_Click(object sender, EventArgs e)
        {
            secondform.Show();
            
         }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'studentdbDataSet1.result' table. You can move, or remove it, as needed.
            this.resultTableAdapter.Fill(this.studentdbDataSet1.result);

        }

    }
}
Posted
Updated 24-Aug-12 11:30am
v2
Comments
[no name] 24-Aug-12 7:30am    
The easiest thing for you to do is to pass the data to form2 or have form2 get the data from a form1 property then bind the data to the grid.
codingisok101 24-Aug-12 7:33am    
my form one contains a list. based on the item selected, the data in form2 that is to be displayed has to change. for this i need to write an sql query. this is where im stuck. i dont know where to write this query.
ridoy 24-Aug-12 7:58am    
it seems you need to output your data to your secondform.is it right? then drag a datagrid from toolbox and choose configure datasource.then in datasouce write your sql query or select data from your tables.not so much difficult..
codingisok101 24-Aug-12 8:00am    
i need to output the data based on the item selected in the list box in one??
Vlad-Dragos 24-Aug-12 8:43am    
I suggest to take some tine and refactor the current architecture.
It does not look good if you are in front of an issue passing data from one view to another view

This is not really 'code so far'. It's a completely unrelated snippet. The Show call makes a modeless form, which is a member variable, so this is good. Now when you do your SQL call in form1, you have a variable you can use to access form2. So pass your data through as a property or through a function call, so that you can display it on your child form.
 
Share this answer
 
Comments
codingisok101 24-Aug-12 7:34am    
amespace final2
{
public partial class Form1 : Form
{
private SqlConnection con;
private SqlCommand command;
private SqlDataAdapter adapter;
private DataSet dataset;
public DataGridView dg;

public Form1()
{
InitializeComponent();

con = new SqlConnection();
command = con.CreateCommand();
con.ConnectionString = "Data Source=CASSINI-003-PC;Initial Catalog=studentdb;Integrated Security=True";
adapter = new SqlDataAdapter(command);
dataset = new DataSet();
}

private void button1_Click(object sender, EventArgs e)
{
command.Parameters.Clear();
command.Parameters.AddWithValue("@StudentID", textBox1.Text);
command.Parameters.AddWithValue("@Name", textBox2.Text);
command.Parameters.AddWithValue("@Age", textBox3.Text);
command.Parameters.AddWithValue("@Gender", textBox4.Text);
command.Parameters.AddWithValue("@Courseno", listBox1.SelectedItem);
command.CommandText = "INSERT into details" + "(StudentID,Name,Age,Gender,Courseno)VALUES" + "(@StudentID,@Name,@Age,@Gender,@Courseno)";

try
{
con.Open();

int result = command.ExecuteNonQuery();

if (result > 0)
MessageBox.Show("student successfully updated");
else
MessageBox.Show("failed to update");
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}

ClearFields();
}

private void button2_Click(object sender, EventArgs e)
{
command.Parameters.Clear();
command.Parameters.AddWithValue("@StudentID", textBox1.Text);
command.CommandText = "SELECT * FROM details WHERE StudentID=@StudentID";

dataset.Tables.Clear();

int result = adapter.Fill(dataset, "details");

if (result > 0)
{
DataRow srow = dataset.Tables["details"].Rows[0];
textBox1.Text = srow["StudentID"].ToString();
textBox2.Text = srow["Name"].ToString();
textBox3.Text = srow["Age"].ToString();
textBox4.Text = srow["Gender"].ToString();
listBox1.SelectedItem = srow["Courseno"].ToString();

}
else
{
MessageBox.Show("Student does not exist");
}

}

void ClearFields()
{
textBox1.Text = String.Empty;
textBox2.Text = String.Empty;
textBox3.Text = String.Empty;
textBox4.Text = String.Empty;


}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string currentitem = listBox1.SelectedItem.ToString();
}

Form2 secondform=new Form2();

private void button3_Click(object sender, EventArgs e)
{
secondform.Show();

}

private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'studentdbDataSet1.result' table. You can move, or remove it, as needed.
this.resultTableAdapter.Fill(this.studentdbDataSet1.result);

}

}
}
Christian Graus 24-Aug-12 7:36am    
Of course you should give your variables better names, this is unreadable. But, it looks like you probably want to pass studentdbDataSet1.result to the contructor of form2, if the data has already been read. Or add a property to pass it whenever you like. I honestly don't see the point of this code dump, it merely allowed me to add a few slightly more specific points to my already complete 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