Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4235228
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:28:09+00:00 2026-05-21T02:28:09+00:00

I have a create account page and on the page I have one button

  • 0

I have a create account page and on the page I have one button to insert all the details into two seperate tables one of the tables Pictures is dependant on the User table 1:1 relationship via UserID.

I have written some code to try get the last insert id so I can insert into the pictures table:

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            try
            {
                OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=commando;");
cn.Open();

                OdbcCommand cmd = new OdbcCommand("INSERT INTO User (Email, FirstName, SecondName, DOB, Location, Aboutme, username, password) VALUES ('" + TextBox1.Text + "', '" + TextBox2.Text + "', '" + TextBox3.Text + "', '" + TextBox4.Text + "', '" + TextBox5.Text + "', '" + TextBox6.Text + "', '" + TextBox7.Text + "', '" + TextBox8.Text + "')", cn);
                OdbcCommand sc = new OdbcCommand("SELECT LAST_INSERT_ID()", cn);
                //convert LAST INSERT into string theUserId

                string filenameDB = Path.GetFileName(FileUpload1.FileName);
                string fileuploadpath = Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/") + Path.GetFileName(FileUpload1.FileName);
                FileUpload1.SaveAs(fileuploadpath);
                string fileuploadpaths = ("~/userdata/" + theUserId + "/uploadedimage/") + filenameDB;
                Label10.Text = "Upload status: File uploaded!";
                OdbcCommand cm = new OdbcCommand("INSERT INTO Pictures (picturepath, UserId) VALUES ('" + fileuploadpaths + "', " + theUserId + ")", cn);

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Label10.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;

            }
            //e.Authenticated = true;
            //Response.Redirect("Login.aspx");
            // Event useradded is true forward to login
        }
    }
}

Not sure if this is correct and I also need to know how to convert the select statement into a string so I can retrieve the UserID from the User table, see database structure:

enter image description here

EDIT

protected void Button1_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile)
    {
        try
        {
            OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=commando;");


            OdbcCommand cmd = new OdbcCommand("INSERT INTO User (Email, FirstName, SecondName, DOB, Location, Aboutme, username, password) VALUES ('" + TextBox1.Text + "', '" + TextBox2.Text + "', '" + TextBox3.Text + "', '" + TextBox4.Text + "', '" + TextBox5.Text + "', '" + TextBox6.Text + "', '" + TextBox7.Text + "', '" + TextBox8.Text + "')SELECT LAST_INSERT_ID()", cn);
            //OdbcCommand sc = new OdbcCommand("SELECT LAST_INSERT_ID()", cn);
            //convert LAST INSERT into string theUserId
            //using (DataTable dt = DataTier.ExecuteQuery(cmd))
            ////error for datatable and datatier
            //if (dt.Rows.Count == 1)
            //{
            //    //Read the new ID from the record that has just been inserted
            //    string theUserId = dt.Rows[0]["UserID"].ToString();
            using (OdbcDataReader reader = cmd.ExecuteReader())
            {
           
            string theUserId = String.Format("{0}", reader.GetString(0));

            string filenameDB = Path.GetFileName(FileUpload1.FileName);
            string fileuploadpath = Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/") + Path.GetFileName(FileUpload1.FileName);
            FileUpload1.SaveAs(fileuploadpath);
            string fileuploadpaths = ("~/userdata/" + theUserId + "/uploadedimage/") + filenameDB;
            Label10.Text = "Upload status: File uploaded!";
            OdbcCommand cm = new OdbcCommand("INSERT INTO Pictures (picturepath, UserId) VALUES ('" + fileuploadpaths + "', " + theUserId + ")", cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            }
        }
        catch (Exception ex)
        {
            Label10.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;

        }
        //e.Authenticated = true;
        //Response.Redirect("Login.aspx");
        // Event useradded is true forward to login
    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T02:28:10+00:00Added an answer on May 21, 2026 at 2:28 am

    You can use a query similar to the following to post a record, then get the ID.

     string sQuery = @"INSERT INTO [ExpenseType]
                                          (
                                             [ExpenseTypeName]
                                            ,[Deleted]
                                            ,[IsTaxable]
                                            ,[UpdatedDate]
                                            ,[UpdatedUser]
                                            ,[ParentCategoryComponentID]
                                            ,[CategoryComponentID]
                                            ,[NLNominalAccountID]
                                            ,[SYSTaxCodeID]
                                          )
                                          VALUES
                                          (
                                             @ExpenseTypeName
                                            ,@Deleted
                                            ,@IsTaxable
                                            ,@UpdatedDate
                                            ,@UpdatedUser
                                            ,@ParentCategoryComponentID
                                            ,@CategoryComponentID
                                            ,@NLNominalAccountID
                                            ,@SYSTaxCodeID
                                          )
                                          SELECT SCOPE_IDENTITY() AS 'ID' ";
    
                        using ( SqlCommand oSqlCommand = new SqlCommand( sQuery ) )
                        {
                            oSqlCommand.Parameters.AddWithValue( "@ExpenseTypeName", this.ExpenseTypeName );
                            oSqlCommand.Parameters.AddWithValue( "@Deleted", this.Deleted );
                            oSqlCommand.Parameters.AddWithValue( "@IsTaxable", this.IsTaxable );
                            oSqlCommand.Parameters.AddWithValue( "@UpdatedDate", base.GetUpdatedDate() );
                            oSqlCommand.Parameters.AddWithValue( "@UpdatedUser", base.GetUpdatedUser() );
                            oSqlCommand.Parameters.AddWithValue( "@ParentCategoryComponentID", this.ParentCategoryComponentID );
                            oSqlCommand.Parameters.AddWithValue( "@CategoryComponentID", this.CategoryComponentID );
                            oSqlCommand.Parameters.AddWithValue( "@NLNominalAccountID", this.NLNominalAccountID );
                            oSqlCommand.Parameters.AddWithValue( "@SYSTaxCodeID", this.SYSTaxCodeID );
    
                            using ( DataTable dt = DataTier.ExecuteQuery( oSqlCommand ) )
                            {
                                if ( dt.Rows.Count == 1 )
                                {
                                    //Read the new ID from the record that has just been inserted
                                                                    string RecordID =  dt.Rows[ 0 ][ "ID" ].ToString();
                                }
                            }
                        }
    

    Note the SELECT SCOPE_IDENTITY() AS ‘ID’ at the end of the query.

    and

              if ( dt.Rows.Count == 1 )
                            {
                                //Read the new ID from the record that has just been inserted
                                string RecordID =  dt.Rows[ 0 ][ "ID" ].ToString();
                            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Create new account page. The form is in page named Register.php
I have a page that will display one of two options for a Struts2
I have this authnav='<li class=last><a href=auth/login>login</a></li>'+ '<li><a href=auth/create_account>create account</a></li>'; It works fine in Firefox,
I have something like this: create table account ( id int identity(1,1) primary key,
I have a website that allows a person to create an account with username/password.
I have a web application that allows users to create an account, and in
I am a Drupal beginner. When users create their account, they have the option
i want to create a messenger and i have those models: Account Message Message
I have a ASP.Net create user wizard. One of the additional steps I included
Here is my situation. I have a web page for users to create their

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.