Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm making a student enrollment system using visual studio 2019 and SQL server management studio 2008.When I clicked the insert button,'Inserted Successfully',there's no error also when i click the registration button,'Record Updated Successfully',there also no error.But when i refresh the database table,there's no data in the database table.Any support for this issue much appreciated

What I have tried:

private void button2_Click(object sender, EventArgs e)
       {


           try
           {

               //taking data from the GUI

               string ID = textBox1.Text;
               string RegistrationNumber = textBox1.Text;
               string StudentName = textBox2.Text;
               string DateOfBirth = dateTimePicker1.Text;
               String Age = textBox3.Text;
               String Gender;
               if (radioButton1.Checked == true)
               {
                   Gender = "Male";
               }
               else
               {
                   Gender = "Female";
               }
               string ContactNumber = textBox4.Text;
               ;


               if (textBox1.Text == "" && textBox2.Text == "" && textBox3.Text == "" && textBox4.Text == "")

               {
                   MessageBox.Show("Complete the Missing Data");
               }
               else if (comboBox1.SelectedItem == null)
               {
                   MessageBox.Show("Click on the selected item after selecting a course");
               }
               else
               {
                   string course = (comboBox1.SelectedItem != null) ? comboBox1.SelectedItem.ToString() : "";
                   MessageBox.Show("Student Inserted Successfully!!");
                   string constr = (ConfigurationManager.ConnectionStrings["dbo.Table_1"] != null) ? ConfigurationManager.ConnectionStrings["dbo.Table_1"].ConnectionString : "";
                   connection = new SqlConnection("Data Source=.\\(localdb)\\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
                   using (SqlConnection con = new SqlConnection(constr))
                       con.ConnectionString = constr;

               }
               if (con.State == ConnectionState.Closed)
                   con.Open();

               SqlCommand com = new SqlCommand("INSERT INTO dbo.Table_1(ID, Registration Number, Student Name, Date of Birth, Age, Gender, Contact Number, Course Enrolled In) VALUES(@ID,@RegistrationNumber,@StudentName,@DateOfBirth,@Age,@Gender,@ContactNumber)", connection);

               com.CommandType = CommandType.Text;
               com.Connection = con;

               com.CommandText = "SELECT * FROM dbo.Table_1 WHERE ID = @ID;";
               com.Parameters.AddWithValue("@ID", textBox1.Text);
               com.Parameters.AddWithValue("@RegistrationNumber", textBox1.Text);
               com.Parameters.AddWithValue("@StudentName", textBox2.Text);
               com.Parameters.AddWithValue("@DateOfBirth", dateTimePicker1.Text);
               com.Parameters.AddWithValue("@Age", textBox3.Text);
               com.Parameters.AddWithValue("@Gender", Gender);
               com.Parameters.AddWithValue("@ContactNumber", textBox4.Text);

               com.ExecuteNonQuery();
               com.ExecuteReader();
               com.Dispose();


           }
           catch
           {
               MessageBox.Show("Error");
           }
           finally
           {
               con.Close();
           }




           }


private void button6_Click(object sender, EventArgs e)
{
    string ID = textBox1.Text;
    if (ID == null) ;
    if (textBox1.Text=="" || textBox2.Text=="" || textBox3.Text=="" || textBox4.Text=="")
    {
        MessageBox.Show("Please Enter Missing Details");
    }
    else
    {
        MessageBox.Show("Record Updated Successfully!!");
        string constr = (ConfigurationManager.ConnectionStrings["dbo.Table_1"] != null) ? ConfigurationManager.ConnectionStrings["dbo.Table_1"].ConnectionString : "";
        using (SqlConnection con = new SqlConnection(constr))
            con.ConnectionString = constr;
        if(con.State==ConnectionState.Closed)
        {
            con.Open();
        }


        String sql = "SELECT COUNT(*) AS [Count] FROM dbo.Table_1 WHERE ID =@ID";

        SqlCommand cmd = new SqlCommand(sql, con) ;
        cmd.Parameters.AddWithValue("@ID", ID);
        int Id;
        if (!int.TryParse(textBox1.Text, out Id))
        {
            // Report problem to your user
            return;
        }
        SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

        while (sdr.Read())


        {
            if (Convert.ToInt32(sdr["count"]) == 1)
            {
                button2.Enabled = false;
                button1.Enabled = true;
            }
            else
            {
                button2.Enabled = true;
                button1.Enabled = false;
             }


        }

        {

        }
    }
    con.Close();
}
Posted
Updated 24-Jul-21 6:33am

1 solution

That's some odd code - there are a lot of things wrong there, not the least being that you show an "Inserted correctly!" message before you even try to do the INSERT

Then when you do the insert, you follow it with a ExecuteReader - and discard the results you won't get anyway!
com.Parameters.AddWithValue("@Gender", Gender);
com.Parameters.AddWithValue("@ContactNumber", textBox4.Text);

com.ExecuteNonQuery();
com.ExecuteReader();
com.Dispose();

But the problem you have noticed is probably simple: the connection you thought you created, you throw away almost immediately:
using (SqlConnection con = new SqlConnection(constr))
    con.ConnectionString = constr;
because that is what using does: disposes of the object at the end of the using block.

So ... if you are inserting data, it's not exactly obvious what you are inserting it too ...
 
Share this answer
 
v2
Comments
AEmu1 DD 24-Jul-21 18:52pm    
Hello sir , can you please explain the registration(update) button cause when i tried to update the database table it's also won't update[i entered data in to database table manually] also could you please tell me how i put two radio buttons[male and female] in to one parameter.Much appreciated your reply
OriginalGriff 25-Jul-21 0:54am    
Read what I said above and apply it to both pieces of code...

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