Hi,
I am using stored procedure to check whether the data is exist in the table or not, here i am not able to return value from database to my function in .net.
And also want to how to store multiple value of select query in a variable because i want to perform some calculation in stored procedure.
Please Help! :)
Loading

Ankit NandekarPosted Apr 11, 2011, 7:19 AM
select @Sname=Sname,@City=city from student where Sno=@Sno
here Sname City are output perameters
Vijay YadavPosted Apr 11, 2011, 9:13 AM
Ankit NandekarPosted Apr 11, 2011, 8:37 AM
Vijay YadavPosted Apr 11, 2011, 8:34 AM
Ankit NandekarPosted Apr 11, 2011, 8:28 AM
Vijay YadavPosted Apr 11, 2011, 8:15 AM
Ankit NandekarPosted Apr 11, 2011, 8:03 AM
Vijay YadavPosted Apr 11, 2011, 7:09 AM
In stored procedure, i want to store data of select query in a variable
For ex: set @Id = (select EmpId from Emp where EmpId=@EmpId)
In this example, i am able to get the EmpId in a variable named @Id
but instead of one column i want to fetch three columns data(i.e. EmpId, Emp_Name,City) and i want to create three variables @EmpId,@EmpName, and @City so that it will store the select query data in these variable for ex, my select is like this:
select EmpId, Emp_Name,City from Emp where EmpId=@EmpId
I hope you will get this time! :)
Mayur GujrathiPosted Apr 11, 2011, 6:59 AM
Vijay YadavPosted Apr 11, 2011, 6:45 AM
For ex:- for one column, i have written the store procedure, like this:
Declare @Id varchar(50)
set @Id = (select EmpId from Emp where EmpId=@EmpId) and its working fine but if there is more column than how to get multiple columns in a variable for ex:
select EmpId, Emp_Name,City from Emp where EmpId=@EmpId
???
Vijay YadavPosted Apr 11, 2011, 6:36 AM
Ankit NandekarPosted Apr 11, 2011, 6:23 AM
public int SP_AdminLogin(string strUserName, string strPassword)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString);
con.Open();
SqlCommand cmdPro = new SqlCommand();
cmdPro.Connection = con;
cmdPro.CommandType = CommandType.StoredProcedure;
cmdPro.CommandText = "SP_AdminLogin";
cmdPro.Parameters.AddWithValue("@UserName", strUserName);
cmdPro.Parameters.AddWithValue("@Pwd", strPassword);
cmdPro.Parameters.Add("@OutputTest", SqlDbType.Int).Direction = ParameterDirection.Output;
cmdPro.ExecuteNonQuery();
int returnvalue= Convert.ToInt32( cmdPro.Parameters["@OutputTest"].Value);
return returnvalue;
}
if problem solved then mark as accepted answer.
Mayur GujrathiPosted Apr 11, 2011, 5:50 AM
Vijay YadavPosted Apr 11, 2011, 5:36 AM
I have tried both the solutions but both of them didn't work, throeing error Object reference not set to an instance of an object even though i have changed cmd to cmdPro. Any other solution???
Mayur GujrathiPosted Apr 11, 2011, 4:27 AM
Mayur GujrathiPosted Apr 11, 2011, 4:16 AM
cmd.Parameters.Add("@OutputTest", SqlDbType.int).Direction = ParameterDirection.Output;
string try = cmd.Parameters["@OutputTest"].Value.ToString();
Mayur GujrathiPosted Apr 11, 2011, 3:58 AM
Vijay YadavPosted Apr 11, 2011, 3:25 AM
thanks for reply,
i changed it to ParameterDirection.OutPut but it gives result zero only whether the data exist or not.
I want to it should return 1 or 2 that i have set to the @OutputTest. Any idea?
Mayur GujrathiPosted Apr 11, 2011, 3:08 AM
ParameterDirection.OutPut
Karthikeyan AnbarasanPosted Apr 11, 2011, 2:28 AM
Vijay YadavPosted Apr 11, 2011, 1:58 AM
It is throwing error on this line:
Mayur GujrathiPosted Apr 11, 2011, 1:37 AM
form1 obj=new form
or as reportdocument obj=new reportdocument
Vijay YadavPosted Apr 11, 2011, 1:15 AM
I am getting error, Object reference not set to an instance of an object. Any idea?
Mayur GujrathiPosted Apr 11, 2011, 1:00 AM
CREATE PROCEDURE dbo.spInputAndOutputTest @InputTest int, @OutputTest int OUTPUT AS SET @OutputTest = @InputTest * 2
and in front end