Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i wrote the code for the above operations to be performed is below



C#
public partial class insertupdatedeletepractice : System.Web.UI.Page
{
    string cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection con; SqlCommand cmd; SqlDataSource ds; SqlParameter P1;
       
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection con = new SqlConnection(cs);
            SqlCommand cmd = new SqlCommand("InsertRecord", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@PEmployeeId", TextBox1.Text);
            cmd.Parameters.AddWithValue("@PEmployeeName", TextBox2.Text);
            cmd.Parameters.AddWithValue("@PEmployeeDesignation", TextBox3.Text);
            cmd.Parameters.AddWithValue("@PEmployeeDOJ", TextBox4.Text);
            cmd.Parameters.AddWithValue("@PEmployeeSalary", TextBox5.Text);
            cmd.Parameters.AddWithValue("@PEmployeeDepartmentno", TextBox6.Text);
            con.Open();
            int i = cmd.ExecuteNonQuery();
            con.Close();
            Console.WriteLine("Data Inserted");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
 
}
    protected void btnupdate_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection con = new SqlConnection(cs);
            SqlCommand cmd = new SqlCommand("UpdateRecord", con);
            cmd.CommandType = CommandType.StoredProcedure;
            P1 = new SqlParameter("@PEmployeeId", SqlDbType.Int);
            P1.Value = Convert.ToInt32(TextBox1.Text);
            cmd.Parameters.Add(P1);
            P1 = new SqlParameter("@PEmployeeDesignation", SqlDbType.VarChar);
            P1.Value = Convert.ToInt32(TextBox2.Text);
            cmd.Parameters.Add(P1);
            P1 = new SqlParameter("@PEmployeeDOJ", SqlDbType.Date);
            P1.Value = Convert.ToInt32(TextBox3.Text);
            cmd.Parameters.Add(P1);
            P1 = new SqlParameter("@PEmployeeDesignation", SqlDbType.VarChar);
            P1.Value = Convert.ToInt32(TextBox4.Text);
            cmd.Parameters.Add(P1);
            P1 = new SqlParameter("@PEmployeeSalary", SqlDbType.Money);
            P1.Value = Convert.ToInt32(TextBox5.Text);
            cmd.Parameters.Add(P1);
            P1 = new SqlParameter("@PEmployeeDepartmentno", SqlDbType.Int);
            P1.Value = Convert.ToInt32(TextBox6.Text);
            cmd.Parameters.Add(P1);
            con.Open();
            int i = cmd.ExecuteNonQuery();
            con.Close();
            Console.WriteLine("Data Updated");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

    }
    protected void btnfind_Click(object sender, EventArgs e)
    {
        try
        {

            SqlConnection con = new SqlConnection(cs);
            SqlCommand cmd = new SqlCommand("GetData", con);
            cmd.CommandType = CommandType.StoredProcedure;
            P1 = new SqlParameter("@PEmployeeId",SqlDbType.Int);
            P1.Value = Convert.ToInt32(TextBox1.Text);
            cmd.Parameters.Add(P1);
            P1 = new SqlParameter("@PEmployeeName", SqlDbType.VarChar);
            P1.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(P1);
            P1 = new SqlParameter("@PEmployeeDesignation", SqlDbType.VarChar);
            P1.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(P1);
            P1 = new SqlParameter("@PEmployeeDOJ", SqlDbType.Date);
            P1.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(P1);
            P1 = new SqlParameter("@PEmployeeSalary", SqlDbType.Money);
            P1.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(P1);
            P1 = new SqlParameter("@PEmployeeDepartmentno", SqlDbType.Int);
            P1.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(P1);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            TextBox2.Text = cmd.Parameters["@PEmployeeName"].ToString();
            TextBox3.Text = cmd.Parameters["@PEmployeeDesignation"].ToString();
            TextBox4.Text = cmd.Parameters["@PEmployeeDOJ"].ToString();
            TextBox5.Text = cmd.Parameters["@PEmployeeSalary"].ToString();
            TextBox6.Text = cmd.Parameters["@PEmployeeDepartmentno"].ToString();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

    }
    protected void btndelete_Click(object sender, EventArgs e)
    {
        try
        {
            cmd = new SqlCommand("DeleteRecord", con);
            cmd.CommandType = CommandType.StoredProcedure;
            P1 = new SqlParameter("@PEmployeeId", SqlDbType.Int);
            cmd.Parameters.AddWithValue("@PEmployeeId", TextBox1.Text);
            P1.Value = Convert.ToInt32(TextBox1.Text);
            cmd.Parameters.Add(P1);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            Console.WriteLine("Record Deleted");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

the problem is inserting is working very fine but remaining operations are not working at all why? please tell me and correct me thanks in advance
Posted
Updated 10-Feb-14 0:56am
v2
Comments
Kornfeld Eliyahu Peter 10-Feb-14 6:57am    
'not working' - in what way?
ravikhoda 10-Feb-14 6:58am    
what errors do you get on each operation...also please post your sql code also i.e. your stored procedure for all above operations. so we get some more idea.

1 solution

No, we can't work this way, without any information about the exact issue.
Please debug your code while each operation is done and check line by line whether it is working as expected or not.

As you have handled Exceptions by Try Catch Block, so you can easily see if there are any Exceptions or not while debugging.

If everything is fine and operations are not performed, then there is something wrong with the query. So, find all problems and correct them.

You will come to know that the debugging process is very interesting.
 
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