Declaring id as int RRS feed

  • Question

  • User-45723355 posted

    Good afternoon everybody in here,

    My name is Peter and I recently joined this forum to seek help in solving a problem I faced. There is a thread I discovered earlier this morning and i found it is similar to what I have been trying to do. 

    I am trying to know exactly where to place my declaration in the C# window of my work

    For example, where can I put this statement ?


     I found this while searching through google, and I had to trace it to this platform. So I applied it to my work and found that there were underlined errors in the declaration statement and the SET statement

    DECLARE @userId INT
    INSERT INTO User (Email, Pass, ....) VALUES ('aaaa@gmail.com', 'pass',.......)
    SET @userId = @@IDENTITY
    INSERT INTO Wallet (Uid, amount) VALUES (@MYID,0)

    i also discovered that in getting this, I will have to create stored procedure but I don't want to use stored procedure in my work.

    So please how and where do I place the statement in my C# window?

    thank you

    Thursday, July 2, 2020 1:29 PM

All replies

  • User-821857111 posted

    For example, where can I put this statement ?


    That's not C#. That's T-SQL. 

    Perhaps you should describe the problem that you are trying to solve, rather than the problems that you are having understanding possible solutions. It may be that the solution you have found is not correct for your problem, for example.

    Thursday, July 2, 2020 1:45 PM
  • User348806598 posted

    You can try something like-

    public int CreateAlbum(string _titel, string _name, string _thumb, int _userid)
        // define return value - newly inserted ID
        int returnValue = -1;
        // define query to be executed
        string query = @"INSERT INTO tblFotoalbum (fldAlbumHead, fldAlbumName, fldAlbumThumb, fldUserID_FK)
                                  VALUES (@titel, @name, @thumb, @userid);
                         SELECT SCOPE_IDENTITY();"
        // set up SqlCommand in a using block   
        using (objCMD = new SqlCommand(query, connection)) 
            // add parameters using regular ".Add()" method 
            objCMD.Parameters.Add("@titel", SqlDbType.VarChar, 50).Value = _titel;
            objCMD.Parameters.Add("@name", SqlDbType.VarChar, 100).Value = _name;
            objCMD.Parameters.Add("@thumb", SqlDbType.VarChar, 100).Value = _thumb;
            objCMD.Parameters.Add("@userid", SqlDbType.VarChar, 25).Value = _userid;
            // open connection, execute query, close connection
            object returnObj = objCMD.ExecuteScalar();
            if(returnObj != null)
               int.TryParse(returnObj.ToString(), out returnValue);
        // return newly inserted ID
        return returnValue;

    Here SELECT SCOPE_IDENTITY() is same as SET @userId = @@IDENTITY.

    returnValue in this case will be similar to userId. You can use returnValue to insert data to your Wallet table.

    Thursday, July 2, 2020 2:13 PM