Wrox Programmer Forums
|
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 11th, 2003, 07:58 PM
Authorized User
 
Join Date: Jun 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to gumgak
Default

below i copied the whole code. (i cut down some irrelevant code to save the space)


================================================== ====================================


<%@ Page Language="C#" ContentType="text/html" debug="true" ResponseEncoding="utf-8" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script language="C#" runat="server" >
SqlConnection objConnection = null;
SqlCommand objCmd = null;
int Identity;

void Page_Load()
{
// make connection string

String strSQL;
String strConnection = "user id=XXX;password=YYY;";
strConnection += "initial catalog=ZZZ;server=XYZ;";

objConnection = new SqlConnection(strConnection);
objConnection.Open();

DataSet ds = new DataSet();
SqlDataAdapter daStates = new SqlDataAdapter("SELECT * FROM states ORDER BY stateName;", objConnection);
daStates.Fill(ds, "states");
stateList.DataSource = ds.Tables["states"];
stateList.DataBind();

}


private void Insert1(object sender, EventArgs e)
{

// set the SQL string
string strSQL = "INSERT INTO DCConfPerson ( " +
                         "firstName, middleName, lastName, " +
                         "address1, address2, city, state, zip, " +
                         "homePhone, mobilePhone, otherPhone) " +
                "VALUES (" +
                          firstName.Text + "', '" +
                          middleName.Text + "', '" +
                          lastName.Text + "', '" +
                          title.Text + "', '" +
                          address1.Text + "', '" +
                          address2.Text + "', '" +
                          city.Text + "', '" +
                          stateList.SelectedItem + "', '" +
                          zip.Text + "', '" +
                          homePhone.Text + "', '" +
                          mobilePhone.Text + "', '" +
                          otherPhone.Text + ");" +
                "SELECT @@IDENTITY AS 'Identity';";

// Create the Command and set its properties
SqlCommand objCmd = new SqlCommand(strSQL, objConnection);

// save Identity in a cookie
HttpCookie RegCookie = new HttpCookie("NewId");
RegCookie.Value = Convert.ToString(Identity);
Response.Cookies.Add(RegCookie);

// execute the command
objCmd.ExecuteNonQuery();

if (objConnection.State == ConnectionState.Open);
objConnection.Close();

Response.Redirect("register2.aspx");

}


</script>
 
Old June 11th, 2003, 08:11 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

ok, well now I know why I am so confused... Your doing c# and I am thinking vb.net..

You have a line

convert.tostring(identity);

However, you never populate identity. You execute the sql command.. but you never take the results of the select and use it.. Imagine when you do a select from a database- you need to look at the result set and pull the data. Sicne I am not good at c# I can't tell you the exact code you need- but you need to get identity after you do you SqlCommand.

Hal Levy
Daddyshome, LLC
 
Old June 25th, 2003, 12:43 PM
Authorized User
 
Join Date: Jun 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to gumgak
Default

instead of using
   "SELECT @@IDENTITY AS 'Identity';";

i used

=================================================

    "SELECT @Identity = @@IDENTITY;";


        // Create the Command and set its properties
        SqlCommand objCmd = new SqlCommand(strSQL, objConnection);

        SqlParameter objOutputParameter =
            new SqlParameter ("@Identity", SqlDbType.Int);
        objCmd.Parameters.Add(objOutputParameter);
        objOutputParameter.Direction = ParameterDirection.Output;
        objCmd.ExecuteReader();
================================================== ===

but i was still unable to get the @@IDENTITY

can anyone help?
 
Old June 29th, 2003, 09:13 PM
Authorized User
 
Join Date: Jun 2003
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi gumgak,

Your code was almost correct:

strSQL = "SELECT @@IDENTITY AS 'Identity';";
SqlCommand objCmd = new SqlCommand(strSQL, objConnection);
SqlDataReader dr = objCmd.ExecuteReader();
dr.Read();
int id = Int32.Parse(dr.GetValue(0).ToString());
dr.Close();

Regards,
Khoa Nguyen





Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing a Variable LarryS Beginning PHP 1 August 8th, 2006 07:52 AM
passing variable tbekarov ASP.NET 2.0 Professional 7 February 1st, 2006 04:16 PM
Passing variable to a public variable Hudson40 Access VBA 2 February 25th, 2005 06:23 AM
Passing a variable mrideout BOOK: Beginning ASP.NET 1.0 3 August 6th, 2004 02:57 PM
Passing a variable trekmp ADO.NET 1 March 4th, 2004 01:57 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.