Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hey!

Im developing a student database and would basically like to use windows form applications and sql databases

i want to design a combobox such that when i select an option, a new window appears and has a list of subjects for which i can enter the marks. how do i do this?

code so far is..

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;
using System.Data.SqlClient;

namespace students
{
    public partial class Form1 : Form
    {
        private SqlConnection con;
        private SqlCommand command;
        private SqlDataAdapter adapter;
        private DataSet dataset;
       

        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.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",comboBox1.Text);
            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 button3_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();
                comboBox1.Text = 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;
            comboBox1.Text = String.Empty;
        }
}
}
Posted
Comments
[no name] 23-Aug-12 3:51am    
What is the error?
You do not need to put whole the code. Where is your problem?
[no name] 23-Aug-12 3:56am    
your code and question are not relevant
codingisok101 23-Aug-12 4:27am    
there is no error in the code. My query is.. now i need to open a second windows form application using the value in the combo box. Im stuck on how to go forward from here.

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