Getting a value out of stored procedure.
Date: 09/06/06
(C Sharp) Keywords: sql
Hi guys, me again.
Here's a stored procedure procedure
[SqlProcedure]
public static void GetLastAddressId()
{
string sqlString = "SELECT MAX(ADRID) FROM dbo.tblAddress";
SqlPipe sp = SqlContext.Pipe;
using (SqlConnection conn = new SqlConnection("context connection=true"))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
cmd.CommandText = sqlString;
string rdr = cmd.ExecuteScalar().ToString();
sp.Send(rdr);
}
}
Then, there is a TableAdapter with query which is set to return a Single Value.
Then, here is a place where I use that TableAdapter to get that maximum Id (inside some class):
public static string getLastAddressId()
{
DALTableAdapters.QueriesTableAdapter fs
= new DALTableAdapters.QueriesTableAdapter();
return (fs.GetLastAddressId()).ToString();
}
Why does the last line (the "return" line) give me an exception, saying that the referenced object doesn't exist? I'm sure I did something wrong with the stored procedure, as I never figured out how to send values like that from stored procedure to my program. Please, help.
Source: http://community.livejournal.com/csharp/75471.html