Developing an employee/student management system with C# windows form application.
Two developers are working on this project, and using two versions of visual studio and SQL servers. (One PC is using VS 2013 Pro and MS SQL express 14, other PC is using VS 2015 enterprise and SQL 14 enterprise). Both PCs are working through team foundation server.
The C# application is connecting with the SQL database and the data retrieval is verified with the login interfaces. Username and passwords are stored in a table and the login process is working fine. So it's sure that the DB connection has no errors.
The problem is that when we tried to insert data into SQL table, C# shows no errors in try/catch exceptions
, but data is not stored in the database. Have used the command.ExecuteNonQuery()
too.
Not sure that the connection string has the problem.
This is the connectionManager
class.
class ConnectionManager
{
public static SqlConnection connection()
{
string connectionString =
@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\tdsdb.mdf;Integrated Security=True";
SqlConnection con = new SqlConnection(connectionString);
con.Open();
return con;
}
}
This is the class which handles the SQL insert operation.
class Student
{
public void addStudent(string studid, string fullname, string nameinit, string gend, string dob, int age, string nic, string nicdate, string hotp, string mobtp, string odlno, string odldt, string odlcls, string reqdlcls, int trtds, int active)
{
string sql= "INSERT INTO tblStudent VALUES ('"+studid+"','"+fullname+"','"+nameinit+"','"+gend+"','"+dob+"','"+age+"','"+nic+"','"+nicdate+"','"+hotp+"','"+mobtp+"','"+odlno+"','"+odldt+"','"+odlcls+"','"+reqdlcls+"','"+trtds+"','"+active+"')";
SqlCommand com = new SqlCommand(sql, ConnectionManager.connection());
com.ExecuteNonQuery();
}
}
This is the class which handles the submit button click operation
private void button6_Click(object sender, EventArgs e)
{
string radtext = "";
bool isChecked = radioButton1.Checked;
if(isChecked)
radtext = radioButton1.Text;
else
radtext = radioButton2.Text;
string hometp = "+94"+textBox8.Text;
string mobiletp = "+94"+textBox9.Text;
int activeint = 1;
string trtdschk = comboBox3.Text;
int trtds = 0;
if (trtdschk == "Yes")
trtds = 1;
else
trtds = 0;
try
{
std.addStudent(textBox2.Text, textBox38.Text, textBox4.Text, radtext, dateTimePicker2.Text, int.Parse(textBox6.Text), textBox7.Text, dateTimePicker3.Text, hometp, mobiletp, textBox12.Text, dateTimePicker4.Text, comboBox1.Text, comboBox2.Text, trtds, activeint);
MessageBox.Show("Personal details has been successfully added to the database!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}