I've searched for days on this and I have found many possibilities with the sql connection being made in the C# code, but I can't find anything where the connection is made using the web.config and .aspx page like mine.
The problem: I have my SQLdatasource1 with an Insert using a stored procedure. The SP checks for a duplicate ID (MRN) and spits out a return of 0 if it finds one and spits out a 1 if it doesn't. It will also perform the insert as long as there is no duplicate. All that is just fine.
However, I want to grab the ReturnValue, check for 0 or 1 and give the user a message depending on the result. Here's what I'm working with. It is abbreviated for simplicity. The form is an asp:formview with an InsertItemTemplate and all controls are contained in it.
<asp:sqldatasource ID="sqldatasource1" runat="server"
ConnectionString="<%$ ConnectionStrings:... %>"
ProviderName="..."
...
<InsertParameters>
<asp:Parameter Name="return_value" DefaultValue="0" Type="Empty" Size="0" ConvertEmptyStringToNull="False" Direction="ReturnValue" />
</InsertParameters>
In the codebehind, I just want to grab the value of the parameter return_value and respond to it. What I have below is what I'd like to do, but it's obviously wrong. I just wanted to post it asI envision it so you'd know my intention. I've tried using the button's click, and that not only gave me the same error that return_value does not exist in current context, but it won't post the message boxes either. I've tried so many different things at this point I'm lost.
UPDATE: This is what I currently have in codebehind. The MessageBox simply shows me "@return_value" when it pops up.
protected void Sqldatasource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
{
SqlParameter return_value = new SqlParameter("@return_value", SqlDbType.Int);
return_value.Direction = ParameterDirection.ReturnValue;
return_value.Value = (object)return_value ?? DBNull.Value;
MessageBox.Show(return_value.Value.ToString());
if (Convert.ToInt32(return_value.Value) != 1)
{
MessageBox.Show(return_value.Value.ToString());
}
else
{
MessageBox.Show(return_value.Value.ToString());
}
}
Thanks in advance. Sorry if this is laughably rudimentary, but I'm flying blind.
UPDATE 2 (5/7/2013): After additional searching and reading, I've tried some other things that haven't worked. No errors, just doesn't work. I've tried the following two bits in my Inserted.
int? return_value = e.Command.Parameters["@return_value"].Value as int?;
MessageBox.Show(return_value.ToString());
and
int return_value = (Convert.ToInt32(e.Command.Parameters["@return_value"]));
MessageBox.Show(return_value.ToString());