Link to home
Start Free TrialLog in
Avatar of palmtreeinfotech
palmtreeinfotechFlag for United States of America

asked on

Asp.Net Set New Session Varilable not Working..

I have the following code in my HEAD section of my .net page.

I am a total newbie on .net but have been in php for a while so I understand variables and a little code.


<%

Session["NewID"] = @DescID;
SqlCommand("@DescID = SCOPE_IDENTITY()");

%>

I'm using an INSERT query to insert the record and carry the new session variable to the next page to post it there and insert it into the database.

I'm getting this error:

Compiler Error Message: CS0103: The name 'DescID' does not exist in the current context

Also, when I get this part working, how do you display the session variable on the next page to retrieve it?  Thanks everyone!
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

If @DescID is an output paramater see the following link:

http://forums.asp.net/t/1197815.aspx/1


you could also do:

Session["NewID"] = cmd.ExecuteScalar();  // change @DescID  = Scope_identity to just select Scope_Identity();


When outputting on a different page:

if (Session["NewID"] != null)
   myTextBox.Text = Session["NewID"].ToString;
Avatar of palmtreeinfotech

ASKER

Hey cool thank you ged325.  I'm getting this error now in the asp.net page browser:

Compiler Error Message: CS0103: The name 'cmd' does not exist in the current context

I am assuming I'm getting this because cmd. is not specified in the code??
Yeah . . . sorry

SqlConnection conn = New SqlConnection(ConfigurationManager.ConnectionStrings["connString"].Tostring());  // or use your existing connection code.
SqlCommand cmd = new SqlCommand(conn);
cmd.CommandText = "MyStoredProc";  // last line in proc is select scope_identity()
cmd.CommandType = CommandType.StoredProcedure;

//USE TRY/CATCH FOR PRODUCTION, this is for display purposes only.
conn.open();
Session["NewID"] =cmd.ExecuteScalar();
conn.close();


if you're not using a stored proc then post your relevant code and  I'll take a look.
Hey no problem.  Like I said, I'm new with .net code but know Sql pretty well.  Here is my code before your last comment:

I embedded it in a code snippet.

I am using Dreamweaver Cs3 because Cs5 doesn't support .net stuff.  I have Visual Studio but don't know how to manuever in there yet and setup dynamic drop-downs, etc.
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="utf-8" %>
<%@ Register TagPrefix="MM" Namespace="DreamweaverCtrl<wbr ></wbr>s" Assembly="DreamweaverCtrls<wbr ></wbr>,version=1<wbr ></wbr>.0.0.0,pub<wbr ></wbr>licKeyToke<wbr ></wbr>n=836f606e<wbr ></wbr>de05d46a,c<wbr ></wbr>ulture=neu<wbr ></wbr>tral" %>


<%


Session["NewID"] = cmd.ExecuteScalar(); 
SqlCommand("SELECT SCOPE_IDENTITY()");

%>

<MM:Insert runat="server"


CommandText='<%# "INSERT INTO dbo.tblReports_Descriptive<wbr ></wbr> (Report_Number, Report_Name, Link_Name, Report_Description, URL, Default_Description) VALUES (?, ?, ?, ?, ?, ?)" %>'
ConnectionString='<%# System.Configuration.Confi<wbr ></wbr>gurationSe<wbr ></wbr>ttings.App<wbr ></wbr>Settings["<wbr ></wbr>MM_CONNECT<wbr ></wbr>ION_STRING<wbr ></wbr>_connectio<wbr ></wbr>n_01"] %>'
DatabaseType='<%# System.Configuration.Confi<wbr ></wbr>gurationSe<wbr ></wbr>ttings.App<wbr ></wbr>Settings["<wbr ></wbr>MM_CONNECT<wbr ></wbr>ION_DATABA<wbr ></wbr>SETYPE_con<wbr ></wbr>nection_01<wbr ></wbr>"] %>'
Expression='<%# Request.Form["MM_insert"] == "form1" %>'
CreateDataSet="false"
SuccessURL='<%# "add2.aspx" %>'
FailureURL='<%# "add_fail.aspx" %>'

>
  <Parameters>
    <Parameter Name="@Report_Number" Value='<%# ((Request.Form["report_num<wbr ></wbr>"] != null) && (Request.Form["report_num"<wbr ></wbr>].Length > 0)) ? Request.Form["report_num"]<wbr ></wbr> : "" %>' Type="WChar" />
    <Parameter Name="@Report_Name" Value='<%# ((Request.Form["report_nam<wbr ></wbr>e"] != null) && (Request.Form["report_name<wbr ></wbr>"].Length > 0)) ? Request.Form["report_name"<wbr ></wbr>] : "" %>' Type="WChar" />
    <Parameter Name="@Link_Name" Value='<%# ((Request.Form["new_link_n<wbr ></wbr>ame"] != null) && (Request.Form["new_link_na<wbr ></wbr>me"].Lengt<wbr ></wbr>h > 0)) ? Request.Form["new_link_nam<wbr ></wbr>e"] : "" %>' Type="WChar" />
    <Parameter Name="@Report_Description"<wbr ></wbr> Value='<%# ((Request.Form["report_des<wbr ></wbr>c"] != null) && (Request.Form["report_desc<wbr ></wbr>"].Length > 0)) ? Request.Form["report_desc"<wbr ></wbr>] : "" %>' Type="WChar" />
    <Parameter Name="@URL" Value='<%# ((Request.Form["report_url<wbr ></wbr>"] != null) && (Request.Form["report_url"<wbr ></wbr>].Length > 0)) ? Request.Form["report_url"]<wbr ></wbr> : "" %>' Type="WChar" />
    <Parameter Name="@Default_Description<wbr ></wbr>" Value='<%# ((Request.Form["default_re<wbr ></wbr>port"] != null) && (Request.Form["default_rep<wbr ></wbr>ort"].Leng<wbr ></wbr>th > 0)) ? Request.Form["default_repo<wbr ></wbr>rt"] : "" %>' Type="WChar" />
  </Parameters>


</MM:Insert>
<MM:PageBind runat="server" PostBackBind="true" />





<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtm<wbr ></wbr>l1/DTD/xht<wbr ></wbr>ml1-transi<wbr ></wbr>tional.dtd<wbr ></wbr>">
<html xmlns="http://www.w3.org/1<wbr ></wbr>999/xhtml"<wbr ></wbr>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add Report - Step 1 of 2</title>
</head>
<body>
<form runat='server' id="form1" name='form1' method='POST' action="">
  <table width="100%" border="1" cellpadding="5" cellspacing="5">
    <tr>
      <td width="28%">Report Name:      </td>
      <td width="72%"><label>
        <textarea name="report_name" id="report_name" cols="100" rows="5"></textarea>
      </label></td>
    </tr>
    <tr>
      <td>Report Description:</td>
      <td><textarea name="report_desc" id="report_desc" cols="100" rows="5"></textarea></td>
    </tr>
    <tr>
      <td>New Link Name:</td>
      <td><input name="new_link_name" type="text" id="new_link_name" size="100" /></td>
    </tr>
    <tr>
      <td>Actual URL Link:</td>
      <td><textarea name="report_url" id="report_url" cols="100" rows="5"></textarea></td>
    </tr>
    <tr>
      <td>Report Number:</td>
      <td><label>
      <input type="text" name="report_num" id="report_num" />
      </label></td>
    </tr>
    <tr>
      <td>Default:</td>
      <td><label>
        <select name="default_report" id="default_report">
          <option value="Y" selected="selected">Y</opt<wbr ></wbr>ion>
          <option value="N">N</option>
        </select>
      </label></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><label>
        <input type="submit" name="button" id="button" value="Go to Step 2 of 2" />
      </label></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1" />
</form>
</body>
</html>

Open in new window

Not that familiar with dream weaver:

but try:

<%

Session["NewID"] = SqlCommand("SELECT SCOPE_IDENTITY()").ExecuteScalar();

%>

I would also move it below the MM:Insert . . . that's what you're trying to get the ID from correct?
Hey thank you.  I'm getting an error now of "the server tag is not well formed" on line 6.  I know it's got to be something simple and yes I put it inside the MM:Insert tag as I want the identity scope of the last inserted record.  What do you think?

<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="utf-8" %>
<%@ Register TagPrefix="MM" Namespace="DreamweaverCtrls" Assembly="DreamweaverCtrls,version=1.0.0.0,publicKeyToken=836f606ede05d46a,culture=neutral" %>



<MM:Insert runat="server"



Session["NewID"] = SqlCommand("SELECT SCOPE_IDENTITY()").ExecuteScalar();
CommandText='<%# "INSERT INTO dbo.tblReports_Descriptive (Report_Number, Report_Name, Link_Name, Report_Description, URL, Default_Description) VALUES (?, ?, ?, ?, ?, ?)" %>'
ConnectionString='<%# System.Configuration.ConfigurationSettings.AppSettings["MM_CONNECTION_STRING_connection_01"] %>'
DatabaseType='<%# System.Configuration.ConfigurationSettings.AppSettings["MM_CONNECTION_DATABASETYPE_connection_01"] %>'
Expression='<%# Request.Form["MM_insert"] == "form1" %>'
CreateDataSet="false"
SuccessURL='<%# "add2.aspx" %>'
FailureURL='<%# "add_fail.aspx" %>'


>
  <Parameters>
    <Parameter Name="@Report_Number" Value='<%# ((Request.Form["report_num"] != null) && (Request.Form["report_num"].Length > 0)) ? Request.Form["report_num"] : "" %>' Type="WChar" />
    <Parameter Name="@Report_Name" Value='<%# ((Request.Form["report_name"] != null) && (Request.Form["report_name"].Length > 0)) ? Request.Form["report_name"] : "" %>' Type="WChar" />
    <Parameter Name="@Link_Name" Value='<%# ((Request.Form["new_link_name"] != null) && (Request.Form["new_link_name"].Length > 0)) ? Request.Form["new_link_name"] : "" %>' Type="WChar" />
    <Parameter Name="@Report_Description" Value='<%# ((Request.Form["report_desc"] != null) && (Request.Form["report_desc"].Length > 0)) ? Request.Form["report_desc"] : "" %>' Type="WChar" />
    <Parameter Name="@URL" Value='<%# ((Request.Form["report_url"] != null) && (Request.Form["report_url"].Length > 0)) ? Request.Form["report_url"] : "" %>' Type="WChar" />
    <Parameter Name="@Default_Description" Value='<%# ((Request.Form["default_report"] != null) && (Request.Form["default_report"].Length > 0)) ? Request.Form["default_report"] : "" %>' Type="WChar" />
  </Parameters>


</MM:Insert>
<MM:PageBind runat="server" PostBackBind="true" />





<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add Report - Step 1 of 2</title>
</head>
<body>
<form runat='server' id="form1" name='form1' method='POST' action="">
  <table width="100%" border="1" cellpadding="5" cellspacing="5">
    <tr>
      <td width="28%">Report Name:      </td>
      <td width="72%"><label>
        <textarea name="report_name" id="report_name" cols="100" rows="5"></textarea>
      </label></td>
    </tr>
    <tr>
      <td>Report Description:</td>
      <td><textarea name="report_desc" id="report_desc" cols="100" rows="5"></textarea></td>
    </tr>
    <tr>
      <td>New Link Name:</td>
      <td><input name="new_link_name" type="text" id="new_link_name" size="100" /></td>
    </tr>
    <tr>
      <td>Actual URL Link:</td>
      <td><textarea name="report_url" id="report_url" cols="100" rows="5"></textarea></td>
    </tr>
    <tr>
      <td>Report Number:</td>
      <td><label>
      <input type="text" name="report_num" id="report_num" />
      </label></td>
    </tr>
    <tr>
      <td>Default:</td>
      <td><label>
        <select name="default_report" id="default_report">
          <option value="Y" selected="selected">Y</option>
          <option value="N">N</option>
        </select>
      </label></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><label>
        <input type="submit" name="button" id="button" value="Go to Step 2 of 2" />
      </label></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1" />
</form>
</body>
</html>

Open in new window

The select scope_identity needs to go after the insert but before the transfer of the page.  Are there events in dreamweaver?  You may want to request attention to get this re-zoned in there.  I could walk you through how to do it in Visual studios but this is different than anything I've seen before.
Yeah I shouldn't be using Dreamweaver for this.  But in VS I made a web form page and pulled over the dynamic dropdown and looked everywhere for the properties of how to tie it to a database and couldn't figure it out. :)

I'm getting the same error even with it moved.  It's weird code I guess using Dreamweaver to do it.
ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you so much I appreciate it.  I have been playing with VS a little bit and it's kind of coming together.  Just have to figure out how to insert a record, etc. and I will switch it over.  It's not as bad as I thought it would be :)