Retrieve new record's ID (using c#)

Results 1 to 2 of 2

Thread: Retrieve new record's ID (using c#)

  1. #1
    Join Date
    Dec 1969

    Default Retrieve new record's ID (using c#)

    Doing this in ASP isn&#039;t too hard. In dotnet - it seems quite convoluted. Could anyone help me with this? I&#039;m just trying to insert two fields and then retrieve the new record&#039;s ID.<BR><BR>My code looks like:<BR><BR>using System;<BR>using System.Web;<BR>using System.Web.UI;<BR>using System.Web.UI.WebControls;<BR>using System.Web.UI.HtmlControls;<BR>using System.Data;<BR>using System.Data.SqlClient;<BR>using System.Configuration;<BR><BR>public class newSurvey : System.Web.UI.Page {<BR> <BR> public System.Web.UI.WebControls.TextBox sTitle;<BR> public System.Web.UI.WebControls.TextBox sDescription;<BR> public System.Web.UI.WebControls.Button AddSurvey;<BR> <BR> public void AddSurvey_Click(Object sender, EventArgs E)<BR> {<BR> if (Page.IsValid)<BR> {<BR> SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);<BR> String insertCmd = "insert into tblSurveys (sTitle, sDescription) values (@sTitle, @sDescription); SELECT @thisId=SCOPE_IDENTITY() FROM tblSurveys";<BR> SqlCommand myCommand = new SqlCommand(insertCmd, myConnection);<BR> <BR> myCommand.Parameters.Add(new SqlParameter("@sTitle", SqlDbType.NVarChar, 200));<BR> myCommand.Parameters["@sTitle"].Value = sTitle.Text;<BR> <BR> myCommand.Parameters.Add(new SqlParameter("@sDescription", SqlDbType.NText, 16));<BR> myCommand.Parameters["@sDescription"].Value = sDescription.Text;<BR> <BR> SqlParameter returnParam = myCommand.Parameters.Add(new SqlParameter("@tId",SqlDbType.Int)); <BR> returnParam.Direction = ParameterDirection.Output; <BR> <BR> myCommand.Connection.Open();<BR> myCommand.ExecuteScalar();<BR> //myCommand.ExecuteNonQuery();<BR> //m_Id = (int)m_SqlCommand.Parameters["@tId"].Value;<BR> myCommand.Connection.Close();<BR> <BR> } // End if page is valid<BR> } // End add survey<BR>} // End public class

  2. #2
    Join Date
    Dec 1969

    Default RE: Retrieve new record's ID (using c#)

    what database?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts