Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hey,

I am working with windows form applications.

I would like to display a the contents of a table in a gridview when a particular item from a list is selected.

how do i go about doing this?

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();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            
            String query = "SELECT * FROM results WHERE Courseno='"+listBox1.SelectedValue+"'";

            con.Open();

            adapter = new SqlDataAdapter(query, con);
            adapter.Fill(dataset);
            dataGridView1.DataSource = dataset;
            
        }

        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);

        }

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

        }

        private void fillByToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.resultsTableAdapter.FillBy(this.studentdbDataSet2.results);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }

        private void fillBy1ToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.resultsTableAdapter.FillBy1(this.studentdbDataSet2.results);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }

        
    }
}
Posted
Updated 24-Aug-12 20:07pm
v3
Comments
[no name] 25-Aug-12 1:47am    
what you try .... what the problem you faced ? please clarify
codingisok101 25-Aug-12 1:49am    
when i click on the button, i want the sql query to be executed and the resulting table to be displayed on a gridview.

but when i click on the button, it just shows the table colums. no data is displayed.

Just Write This :

C#
private void button3_Click(object sender, EventArgs e)
       {

           String query = "SELECT * FROM results WHERE Courseno='"+listBox1.SelectedValue+"'";

           con.Open();

           adapter = new SqlDataAdapter(query, con);
           adapter.Fill(dataset);
           dataGridView1.DataSource = dataset;
           dataGridView1.Databind();
       }
 
Share this answer
 
Comments
codingisok101 25-Aug-12 1:53am    
i dont have the databind function.
[no name] 25-Aug-12 1:54am    
you apply my solution or Not?
codingisok101 25-Aug-12 1:56am    
i dont have the option of using databind. it isnt recognised.
[no name] 25-Aug-12 2:02am    
please post the design form
if your description is correct,it is clear that your query is executing with out errors.The problem can be there may be no values in the table for this command since it shows the table columns correctly.
C#
String query = "SELECT * FROM results WHERE Courseno='"+listBox1.SelectedValue+"'";


Make sure that the part listBox1.SelectedValue returns a genuine value in the debug mode.
 
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