Hy Everyone! I am trying to add some yes/no values into my sql database by using some radio buttons and checkboxes. when I click the insert button it does not show any error but also does not insert data into the table. I have a DBConn class that has following code:
class DBConn
{
DataSet ds;
int c;
SqlConnection sqlconn;
SqlCommand sqlcmd;
public static string connectionString = @"Data Source=user-PC;Initial Catalog=RecruitmentProject;Integrated Security=True";
public bool IUD(String query)
{
try
{
sqlconn = new SqlConnection();
sqlconn.ConnectionString = connectionString;
sqlconn.Open();
sqlcmd = sqlconn.CreateCommand();
sqlcmd.CommandText = query;
c = sqlcmd.ExecuteNonQuery();
sqlconn.Close();
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}
if (c > 0)
return true;
else
return false;
}
following is my code for insertion:
public void ins_Skills()
{
String cnic = txtCNIC.Text;
String added = lblLoginID.Text;
string query = "Insert INTO ComputerSkills_Table (cnic,ComputerSkills,MSOffice,MSWord,MSExcell,MSPpt,MSAccess,Typing,EnglishType,UrduType,Networking,Programming,[Database],AddedBy) VALUES (@cnic, @ComputerSkills, @MSOffice, @MSWord, @MSExcell, @MSPpt, @MSAccess, @Typing, @EnglishType, @UrduType, @Networking, @Programming, @db, @AddedBy)";
SqlCommand cmd = new SqlCommand(query, conn);
//Pass values to Parameters
if(cmbComp.Text != "No")
{
cmd.Parameters.AddWithValue("@cnic", txtCNIC.Text);
cmd.Parameters.AddWithValue("@ComputerSkills", rbComp.Text);
cmd.Parameters.AddWithValue("@MSOffice", rbOffice.Text);
cmd.Parameters.AddWithValue("@MSWord", rbWord.Text);
cmd.Parameters.AddWithValue("@MSExcell", rbExcell.Text);
cmd.Parameters.AddWithValue("@MSPpt", rbPpt.Text);
cmd.Parameters.AddWithValue("@MSAccess", rbAccess.Text);
cmd.Parameters.AddWithValue("@Typing", rbTyping.Text);
cmd.Parameters.AddWithValue("@EnglishType", rbEngType.Text);
cmd.Parameters.AddWithValue("@UrduType", rbUrduType.Text);
cmd.Parameters.AddWithValue("@Networking", rbNetwork.Text);
cmd.Parameters.AddWithValue("@Programming", rbProgram.Text);
cmd.Parameters.AddWithValue("@db", rbDB.Text);
cmd.Parameters.AddWithValue("@AddedBy", lblLoginID.Text);
}
else if(cmbComp.Text == "No")
{
cmd.Parameters.AddWithValue("@cnic", txtCNIC.Text);
cmd.Parameters.AddWithValue("@ComputerSkills", "No");
cmd.Parameters.AddWithValue("@MSOffice", "No");
cmd.Parameters.AddWithValue("@MSWord", "No");
cmd.Parameters.AddWithValue("@MSExcell", "No");
cmd.Parameters.AddWithValue("@MSPpt", "No");
cmd.Parameters.AddWithValue("@MSAccess", "No");
cmd.Parameters.AddWithValue("@Typing", "No");
cmd.Parameters.AddWithValue("@EnglishType", "No");
cmd.Parameters.AddWithValue("@UrduType", "No");
cmd.Parameters.AddWithValue("@Networking", "No");
cmd.Parameters.AddWithValue("@Programming", "No");
cmd.Parameters.AddWithValue("@db", "No");
cmd.Parameters.AddWithValue("@AddedBy", lblLoginID.Text);
}
try
{
conn.Open();
Console.WriteLine(query);
cmd.ExecuteNonQuery();
MessageBox.Show("Records inserted");
//q = db.IUD(query); //DBConn Function Call
} // try
catch (SqlException ex)
{
MessageBox.Show("Records not inserted", ex.StackTrace);
} // catch
finally
{
conn.Close();
}
now it does not show the data inserted messagebox and neither does it show the not inserted messagebox, its as if the query is not being passed or called. can someone help?
EDIT : I went through it again and realized that the function was not getting called when the Yes radiobutton was marked on the previous form. now it does show the messagebox that "Data inserted" but when i check the table there are no values inserted at all.
code for taking value from radio button is:
private void rbNetwork_CheckedChanged(object sender, EventArgs e)
{
network = rbNetwork.Text;
}
and then I put the network variable with the radiobutton value in the query as the value for network column. but I think the variable has null value thats why nothing is inserted into the table and the table remains as if it was just created with only 1 row and all the columns having null value.
EDIT: (2) I debugged my code again as recommended by some friends here and this is the exception i am getting:
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll