Click here to Skip to main content
16,017,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
DataBase:
SQL
ALTER procedure [dbo].[sp_emp_dept_exam]
(
	@empno int=0,
	@ename nvarchar(50)=null,
	@sal nvarchar(50)=null,
	@deptno int=0,
	@dname nvarchar(50)=null,
	@opr int=0
)
as 
begin
	if @opr=1
	begin
		Insert into emp_exam(ename,sal,deptno)values(@ename,@sal,@deptno)
	end
	else if @opr=2
	begin
		SELECT dname,deptno FROM dept_exam
	end
	else if @opr=3
	begin		
		select ename,dname,sal from emp_exam e inner join dept_exam d on e.deptno=d.deptno
	end
	
end

//Code Behind:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace DemoExample
{
    public partial class test_exam : System.Web.UI.Page
    {   
        SqlCommand cmd;
        SqlDataReader dr;
        SqlDataAdapter da;
        DataSet ds=new DataSet();
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["connstring2"].ToString());
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cmd = new SqlCommand("sp_emp_dept_exam", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@opr", SqlDbType.Int).Value = 2;
                //con.Open();
                da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                ddldname.DataTextField = "dname";
                ddldname.DataValueField = "deptno";
                ddldname.DataSource = ds;
                ddldname.DataBind();
               // con.Close();
            }
        }

        protected void btnsave_Click(object sender, EventArgs e)
        {            
            //cmd = new SqlCommand("Insert into emp_exam(ename,sal)values(@ename,@sal)", con);
            //cmd.Parameters.Add("@ename", txtename.Text);
            //cmd.Parameters.Add("@sal", txtsal.Text);
            //cmd.Parameters.Add("@deptno", ddldname.SelectedValue);

            cmd = new SqlCommand("sp_emp_dept_exam", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("ename", SqlDbType.NVarChar, 50).Value= txtename.Text;
            cmd.Parameters.Add("@sal", SqlDbType.NVarChar, 50).Value = txtsal.Text;
            cmd.Parameters.Add("@deptno", SqlDbType.Int).Value = ddldname.SelectedValue;
            cmd.Parameters.Add("@opr", SqlDbType.Int).Value = 1;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            lblmsg.Text = "Record Successfully Inserted";
            txtename.Text = txtsal.Text = "";
            cmd.Parameters.Clear();           

            cmd.Parameters.Clear();
            cmd.Parameters.Add("@opr", SqlDbType.Int).Value = 3;            
            con.Open();
            //dr=cmd.ExecuteReader();
            //gvexam.DataSource = dr;
            da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            gvexam.DataSource = ds;
            
            gvexam.DataBind();
            con.Close();
        }
    }
}
Posted
Updated 17-Nov-14 22:21pm
v2
Comments
Manas Bhardwaj 18-Nov-14 4:21am    
And the problem is?
OriginalGriff 18-Nov-14 4:26am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.
Arkadeep De 18-Nov-14 5:35am    
share the error....

1 solution

Hello,
If you getting Error in insertion of data then might be below error in this line.
cmd.Parameters.Add("ename", SqlDbType.NVarChar, 50).Value= txtename.Text;
'@' is missing in ename pamperer.there should be @ename not ename.



thanks
 
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