Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            command.Parameters.Clear();
            command.Parameters.AddWithValue("@Courseno", listBox1.SelectedValue);
            command.CommandText = "SELECT * FROM results WHERE Courseno=@Courseno";

            try
            {
                con.Open();
                command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
          
            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);

        }

        
    }
}
the exception thrown is that the query expects the parameter @Courseno which was not supplied.
Posted
Updated 24-Aug-12 3:19am
v3
Comments
Tim Corey 24-Aug-12 8:31am    
Is this all of your code, or have you left something out? I don't see where you are using your command object to make the call to the database.
codingisok101 24-Aug-12 8:35am    
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)
{
command.Parameters.Clear();
command.Parameters.AddWithValue("@Courseno", listBox1.SelectedValue);
command.CommandText = "SELECT * FROM results WHERE Courseno=1";

try
{
con.Open();
command.ExecuteNonQuery();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}

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

}


}
}

this is my
codingisok101 24-Aug-12 8:43am    
this is my code. where am i going wrong?
BillW33 24-Aug-12 8:47am    
For future reference, it would be better to add code to your original question by using the "Improve question" button.
codingisok101 24-Aug-12 8:50am    
sure! thanks for the tip!

Your problem seems to be how you are trying to set up your code. When you initialize your form, you set up your adapter and passed in the command object. Later you put information into that command object but never passed it back into the adapter. I would recommend that you start with a less complicated example. Right now it is difficult to see what is going where and when. Instead, put all of the connection, command, adapter, etc. objects into your method. Create new ones just for that call. Set everything up the way it should be and run your call. That should work. Then I would recommend slowly working back to the model you are trying to get to.

As a side note, I personally am not a fan of class-level variables for connection objects. This leaves the connection open for the duration of the class's lifetime. This will use more resources than is necessary in almost every scenario. I usually try to keep the connections to the database as short in duration as possible. If I'm going to be doing a number of operations at once, I'll keep it open for the lifespan of that set of operations but that is the longest I ever keep one open. Just something to think about.
 
Share this answer
 
C#
private void button3_Click(object sender, EventArgs e)
       {

           //command.Parameters.AddWithValue("@Courseno", listBox1.SelectedValue);
              
           command.CommandText = "SELECT * FROM results WHERE Courseno = " + listBox1.SelectedValue;

           adapter.Fill(dataset);
           dataGridView1.DataSource = dataset;
       }
 
Share this answer
 
v2
Comments
Tim Corey 24-Aug-12 8:30am    
Nope, not a good solution. You shouldn't put the value directly into the SQL string. This could lead to SQL injection. I understand that the listbox might be a set list now, but that might change in the future. I strongly recommend against this technique. Besides, this wouldn't even work, since you would end up comparing the Courseno column against the literal "listbox1.SelectedValue", not its actual value.
codingisok101 24-Aug-12 8:31am    
it says incorrect syntax near '='
Kamalkant(kk) 24-Aug-12 8:44am    
you can store listbox1 value in to another variable and the pass it in to the query

object strval=listBox1.SelectedValue;
command.CommandText = "SELECT * FROM results WHERE Courseno = " +strval ;
it will be like..though it isn't a good process..but can remove your error atleast..
command.CommandText = "SELECT * FROM results WHERE Courseno = '" + listBox1.SelectedValue + "'";
 
Share this answer
 
Comments
codingisok101 24-Aug-12 8:57am    
i tried this. the error is rectified! thanks
ridoy 24-Aug-12 9:05am    
welcome..glad to help you

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