Click here to Skip to main content
15,888,321 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have created a web form in ASP.Net using C# & at there I have created a button to submit data to a table in an SQl Server 2008 express db. The following will be the coding

C#
protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                string connStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\MyProjects\LeaveMgt\LeaveMgt\App_Data\LeaveMgt.mdf;Integrated Security=True;User Instance=True";
                SqlConnection dbCon = new SqlConnection(connStr);
                SqlCommand comm = new SqlCommand();
                dbCon.Open();
                comm.CommandType = CommandType.Text;
                comm.Connection = dbCon;
                comm.CommandText = ("INSERT INTO LeaveCover VALUES('" + txtEmpNo.Text + "','" + txtEmpName.Text + "','" + txtCoverUp.Text + "','" + txtActOff.Text + "', '" + txtDate.Text + "','" + txtLevAdd.Text + "','" + ddlApprove.Text + "', '" + ddlNor.Text + "','" + ddlNotLev.Text + "')");
                comm.ExecuteNonQuery();
                dbCon.Close();
                lblMessage.Text = "Ok";
            }
            catch {

                lblMessage.Text = "Bad";
            
            }

When I click the submit button it always trig the cath{} but I cannot find the error!
My Database is at the following location and the connection string is as the following.

DB@ <pre lang="c#">D:\MyProjects\LeaveMgt\LeaveMgt\App_Data\LeaveMgt.mdf




ConnectionString is
C#
string connStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\MyProjects\LeaveMgt\LeaveMgt\App_Data\LeaveMgt.mdf;Integrated Security=True;User Instance=True";


If you can please show me the error!

Thanks
Chiranthaka
Posted

What is the error? Try catching an exception rather than just "Bad". That is meaningless. Also, you could step through the code and see where it jumps to the catch.
Actually, it kinda looks like you may have to specify the columns like insert into leaevcover (col1, col2, etc) values (0,1,2,etc). If the table has a surrogate key column or other columns you are not inserting data to that are non null.

Also, didn't you just ask this in another way an get an answer like using a stored procedure with parameters, which is the better way?
 
Share this answer
 
Its the sequence of the statements nothing else!
 
Share this answer
 

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