Solved

Asp.Net Set New Session Varilable not Working..

Posted on 2012-04-04
10
270 Views
Last Modified: 2012-08-14
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!
0
Comment
Question by:palmtreeinfotech
  • 5
  • 5
10 Comments
 
LVL 39

Expert Comment

by:Kyle Abrahams
Comment Utility
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;
0
 

Author Comment

by:palmtreeinfotech
Comment Utility
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??
0
 
LVL 39

Expert Comment

by:Kyle Abrahams
Comment Utility
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.
0
 

Author Comment

by:palmtreeinfotech
Comment Utility
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

0
 
LVL 39

Expert Comment

by:Kyle Abrahams
Comment Utility
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?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:palmtreeinfotech
Comment Utility
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

0
 
LVL 39

Expert Comment

by:Kyle Abrahams
Comment Utility
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.
0
 

Author Comment

by:palmtreeinfotech
Comment Utility
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.
0
 
LVL 39

Accepted Solution

by:
Kyle Abrahams earned 500 total points
Comment Utility
Here's a sample of how to populate a drop down in ASP.NEt using visual studios.

http://www.dbtutorials.com/advanced/populatingdropdown-csharp.aspx

Essentially every page is made up of 2 files.  The front end and the back end.  The front end is the layout of the controls, the back end is the hidden part that effects the controls.

Try it in VS, let me know if you have any issues.  

If you need to convert code to vb you can use the following link:
http://www.google.com/url?sa=t&rct=j&q=&esrc=s&frm=1&source=web&cd=1&ved=0CDgQFjAA&url=http%3A%2F%2Fwww.developerfusion.com%2Ftools%2Fconvert%2Fcsharp-to-vb%2F&ei=-aJ9T8jtGcvjrAej89HnDA&usg=AFQjCNFM321pCrxs_o-P69sgwPbmfGp0Rw&sig2=gXEOjjHk7RCI04Sa9ZsUmQ
0
 

Author Closing Comment

by:palmtreeinfotech
Comment Utility
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 :)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now