Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends
this is my stored procedure which have been already posted and now working fine.
SQL
create procedure [dbo].[temptable1] (@branchid int,@academicyearid int)
as
begin
set nocount on

declare @branchname varchar(50)
declare @acfy int ;
declare @acty int
select @branchname=Branch_name from Branch where Branch_id=@branchid
select @acfy=Acadamic_year_from,@acty=Acadamic_year_to from Acadamic_year where Acadamic_year_id=@academicyearid
create table #temptable1(Brach_name varchar(50),Acadamic_year_from smallint,Acadamic_year_to smallint)
insert into #temptable1(Brach_name,Acadamic_year_from,Acadamic_year_to)values(@branchname,@acfy,@acty)
select * from #temptable1;

end


an this is my c# code for calling this procedure.
SqlCommand cmd = new SqlCommand("temptable1", Dob.con);
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Parameters.Add(new SqlParameter("@branchid",r));
               cmd.Parameters.Add(new SqlParameter("@academicyearid",s));
               SqlDataAdapter da=new SqlDataAdapter(cmd);
               DataSet ds=new DataSet();
               da.Fill(ds,"#temptable1");
               DataTable dt=ds.Tables["#temptable1"];
               return dt;

but dataset and datatable return null values.what is the problem
Posted

I have checked the above query. The only reason should be parameters would be incorrect or no records matches the corresponding parameters.

I tried with below scripts and code. Its working. check with this,
DB----
SQL
Create table branch
(branch_id int,
branch_name varchar(10)
)

Create table Acadamic_year
(academic_year_id int,
academic_year_from int,
academic_year_to int)


insert into branch values(1,'MSC')
insert into Acadamic_year values(1,2012,2013)


alter procedure [dbo].[temptable] (@branchid int,@academicyearid int)

as
begin
set nocount on

declare @branchname varchar(50)
declare @acfy int ;
declare @acty int
select @branchname=Branch_name from Branch where Branch_id=@branchid
select @acfy=Academic_year_from,@acty=Academic_year_to from Acadamic_year where Academic_year_id=@academicyearid
create table #temptable(Brach_name varchar(10),Acadamic_year_from smallint,Acadamic_year_to smallint)
insert into #temptable(Brach_name,Acadamic_year_from,Acadamic_year_to)values(@branchname,@acfy,@acty)


select * from #temptable


end

--exec temptable 1,1

C# code:
C#
public partial class Form1 : Form
    {
        private SqlConnection con;
        public Form1()
        {
            InitializeComponent();
            con = new SqlConnection("Data Source=localhost\\sqlexpress; Initial Catalog=Cricket; Integrated Security=SSPI;");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int r=1;
            int s=1;
            SqlCommand cmd = new SqlCommand("temptable", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@branchid", r));
            cmd.Parameters.Add(new SqlParameter("@academicyearid", s));
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "#temptable1");
            DataTable dt = ds.Tables["#temptable1"];

           
        }
    }
 
Share this answer
 
v3
Comments
baskaran chellasamy 28-Jul-12 6:36am    
sorry records available in the database for the corresponding parameter.in sql its working fine. but when i calling to the application then only it return null value.but i correctly pass the brachid and academicyearid to the store procedure. is the temporary table available for dataset when using temporary table to application?
Santhosh Kumar Jayaraman 28-Jul-12 6:41am    
no.It doesnt matter.. It should work, i am not sure why its not working. I tried with below scripts and code. Its working. check with this,

DB----
Create table branch
(branch_id int,
branch_name varchar(10)
)

Create table Acadamic_year
(academic_year_id int,
academic_year_from int,
academic_year_to int)


insert into branch values(1,'MSC')
insert into Acadamic_year values(1,2012,2013)


alter procedure [dbo].[temptable] (@branchid int,@academicyearid int)

as
begin
set nocount on

declare @branchname varchar(50)
declare @acfy int ;
declare @acty int
select @branchname=Branch_name from Branch where Branch_id=@branchid
select @acfy=Academic_year_from,@acty=Academic_year_to from Acadamic_year where Academic_year_id=@academicyearid
create table #temptable(Brach_name varchar(10),Acadamic_year_from smallint,Acadamic_year_to smallint)
insert into #temptable(Brach_name,Acadamic_year_from,Acadamic_year_to)values(@branchname,@acfy,@acty)


select * from #temptable


end
exec temptable 1,1


My C# code.


public partial class Form1 : Form
{
private SqlConnection con;
public Form1()
{
InitializeComponent();
con = new SqlConnection("Data Source=localhost\\sqlexpress; Initial Catalog=Cricket; Integrated Security=SSPI;");
}

private void Form1_Load(object sender, EventArgs e)
{
int r=1;
int s=1;
SqlCommand cmd = new SqlCommand("temptable", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@branchid", r));
cmd.Parameters.Add(new SqlParameter("@academicyearid", s));
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "#temptable1");
DataTable dt = ds.Tables["#temptable1"];


}
}
baskaran chellasamy 28-Jul-12 6:56am    
Thank you very much.its working fine now
Santhosh Kumar Jayaraman 28-Jul-12 7:00am    
What changes you made
Try:
C#
SqlCommand cmd = new SqlCommand("temptable1");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@branchid"));
cmd.Parameters.Add(new SqlParameter("@academicyearid"));
SqlDataAdapter da=new SqlDataAdapter(cmd, Dob.con);
DataSet ds=new DataSet();
da.Fill(ds,"myTable");
DataTable dt=ds.Tables["myTable"];
return dt;


Refer:
MSDN: HOW TO: Call SQL Server Stored Procedures in ASP.NET by Using Visual C# .NET[^]
MSDN: HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET[^]
MSDN: Configuring Parameters and Parameter Data Types (ADO.NET)[^]
 
Share this answer
 
Do not use # while filling Dataset
C#
SqlCommand cmd = new SqlCommand("temptable1", Dob.con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@branchid",r));
                cmd.Parameters.Add(new SqlParameter("@academicyearid",s));
                SqlDataAdapter da=new SqlDataAdapter(cmd);
                DataSet ds=new DataSet();
                da.Fill(ds,"temptable1");
                DataTable dt=ds.Tables["#temptable1"];
                return dt;



Thanks
Ashish
 
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