Click here to Skip to main content
15,887,386 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
AnswerRe: Microsoft surface precision mouse Pin
Marc Clifton29-Jul-20 9:19
mvaMarc Clifton29-Jul-20 9:19 
GeneralRe: Microsoft surface precision mouse Pin
Joan M29-Jul-20 9:28
professionalJoan M29-Jul-20 9:28 
GeneralRe: Microsoft surface precision mouse Pin
David Crow29-Jul-20 9:37
David Crow29-Jul-20 9:37 
GeneralRe: Microsoft surface precision mouse Pin
Joan M29-Jul-20 9:58
professionalJoan M29-Jul-20 9:58 
GeneralRe: Microsoft surface precision mouse Pin
Marc Clifton29-Jul-20 9:58
mvaMarc Clifton29-Jul-20 9:58 
GeneralRe: Microsoft surface precision mouse Pin
Joan M29-Jul-20 10:07
professionalJoan M29-Jul-20 10:07 
GeneralRe: Microsoft surface precision mouse Pin
David Crow29-Jul-20 10:27
David Crow29-Jul-20 10:27 
Generalpassing UserID into other columns Pin
Georgeakpan1329-Jul-20 6:12
Georgeakpan1329-Jul-20 6:12 
I am trying to insert record into two tables and fetch the UserId of first table and insert into another column of the same table and also into another table but it appears that when I insert data into the tables, the Id of the user is returned and inserted into the second table but inserted a different interger into another column of same first table.
For example, I have UserTable and WalletTable and I want to insert records simultaneously into the two tables and at the same time fetch the Id of UserTable and insert into columns of both tables.
Here is the structure of the first Table: I want to fetch “UserID” in UserTable and insert into “CreatedBy” of UserTable and UserID of WalletTable. But after inserting I discovered that interger “-1” is inserted in CreatedBy column of UserTable and on WalletTable the correct UserID is inserted. How can I correct this please?
UserTable:
UserID | email | pass | con_pass | UserRole | Name | CreatedBy | image | CreateDate |
WalletTable:
Id | UserID | email | Name | amount |
Here is my Insert code (C#)
C#
if (mailtxtbx.Text != "" & pass.Text != "" & conpass.Text != "" & txtname.Text !="")
{
    if (pass.Text == conpass.Text)
    {
        if (Filedoc.PostedFile.FileName != "")
        {
            if (check1.Checked)
              {
                int Uid = -1;

                byte[] image;
                Stream s = Filedoc.PostedFile.InputStream;
                BinaryReader br = new BinaryReader(s);
                image = br.ReadBytes((Int32)s.Length);
                // define query to be executed
                string query = @"INSERT INTO Users (email, pass, con_pass, UserRole, Name, CreatedBy, image, CreateDate) VALUES (@email, @pass, @con_pass, @UserRole, @Name, @CreatedBy, @image, @CreateDate);
                SELECT SCOPE_IDENTITY();";
                // set up SqlCommand in a using block
                using (SqlCommand objCMD = new SqlCommand(query, con))
                {
                    // add parameters using regular ".Add()" method
                    objCMD.Parameters.Add("@email", SqlDbType.VarChar, 50).Value = mailtxtbx.Text.Trim();
                    objCMD.Parameters.Add("@pass", SqlDbType.VarChar, 100).Value = pass.Text.Trim();
                    objCMD.Parameters.Add("@con_pass", SqlDbType.VarChar, 50).Value = conpass.Text.Trim();
                    objCMD.Parameters.Add("@UserRole", SqlDbType.VarChar, 50).Value = 'A';
                    objCMD.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = txtname.Text.Trim();
                    objCMD.Parameters.Add("@CreatedBy", SqlDbType.Int, 50).Value = Uid;
                    objCMD.Parameters.Add("@image", SqlDbType.VarBinary).Value = image;
                    objCMD.Parameters.Add("@CreateDate", SqlDbType.DateTime, 100).Value = DateTime.Now;
                    // open connection, execute query, close connection
                    con.Open();
                    object returnObj = objCMD.ExecuteScalar();

                    if (returnObj != null)
                    {
                        int.TryParse(returnObj.ToString(), out Uid);
                    }
                    cmd.ExecuteNonQuery();
                }
                con.Close();

                if (Uid > 0)
                {
                    query = @"INSERT INTO UserWallet (Uid, email, Name, amount) VALUES (@Uid, @email, @Name, @amount)";
                    using (SqlCommand objCMD = new SqlCommand(query, con))
                    {
                        // add parameters using regular ".Add()" method
                        objCMD.Parameters.Add("@Uid", SqlDbType.Int, 50).Value = Uid;
                        objCMD.Parameters.Add("@email", SqlDbType.VarChar, 50).Value = mailtxtbx.Text.Trim();
                        objCMD.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = txtname.Text.Trim();
                        objCMD.Parameters.Add("@amount", SqlDbType.Float, 100).Value = 0; //Change type here accordingly
                        con.Open();
                        object returnObj = objCMD.ExecuteScalar();

                        if (returnObj != null)
                        {
                            int.TryParse(returnObj.ToString(), out Uid);
                        }
                        cmd.ExecuteNonQuery();
                        lblsuccess.Visible = true;
                        Div1.Visible = true;
                        lblsuccess.Text = "Successfully Signed Up";
                        lblsuccess.ForeColor = System.Drawing.Color.Green;
                        lblMessage.Visible = false;
                        dvMessage.Visible = false;
                        mailtxtbx.Text = "";
                        pass.Text = "";
                        conpass.Text = "";
                        txtname.Text = "";
                        // Response.Redirect("Default.aspx");
                    }
                }
                con.Close();
            }

GeneralRe: passing UserID into other columns Pin
User 1106097929-Jul-20 6:16
User 1106097929-Jul-20 6:16 
GeneralRe: passing UserID into other columns Pin
Jacquers29-Jul-20 6:25
Jacquers29-Jul-20 6:25 
GeneralRe: passing UserID into other columns Pin
OriginalGriff29-Jul-20 6:37
mveOriginalGriff29-Jul-20 6:37 
GeneralRe: passing UserID into other columns Pin
User 1106097929-Jul-20 7:01
User 1106097929-Jul-20 7:01 
GeneralRe: passing UserID into other columns Pin
Johnny J.29-Jul-20 7:09
professionalJohnny J.29-Jul-20 7:09 
GeneralRe: passing UserID into other columns Pin
W Balboos, GHB29-Jul-20 7:40
W Balboos, GHB29-Jul-20 7:40 
GeneralRe: passing UserID into other columns Pin
Gerry Schmitz29-Jul-20 8:26
mveGerry Schmitz29-Jul-20 8:26 
GeneralRe: passing UserID into other columns Pin
User 1106097929-Jul-20 8:31
User 1106097929-Jul-20 8:31 
GeneralRe: passing UserID into other columns Pin
Gerry Schmitz29-Jul-20 9:42
mveGerry Schmitz29-Jul-20 9:42 
GeneralRe: passing UserID into other columns Pin
User 1106097929-Jul-20 9:45
User 1106097929-Jul-20 9:45 
GeneralRe: passing UserID into other columns Pin
Richard MacCutchan29-Jul-20 8:45
mveRichard MacCutchan29-Jul-20 8:45 
GeneralRe: passing UserID into other columns Pin
User 1106097929-Jul-20 8:52
User 1106097929-Jul-20 8:52 
GeneralRe: passing UserID into other columns Pin
Nelek29-Jul-20 7:52
protectorNelek29-Jul-20 7:52 
AnswerRe: passing UserID into other columns Pin
ZurdoDev29-Jul-20 8:51
professionalZurdoDev29-Jul-20 8:51 
GeneralRe: passing UserID into other columns Pin
OriginalGriff29-Jul-20 8:58
mveOriginalGriff29-Jul-20 8:58 
GeneralRe: passing UserID into other columns Pin
ZurdoDev29-Jul-20 9:04
professionalZurdoDev29-Jul-20 9:04 
GeneralRe: passing UserID into other columns Pin
User 1106097929-Jul-20 9:00
User 1106097929-Jul-20 9:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.