locked
Add value to variable RRS feed

  • Question

  • User-1499457942 posted

    Hi

     I want to add ID value in fp variable after G_. I have below code

    fp = "G_" + "_" + fp.FileName.ToString();
    trans = con.BeginTransaction("STransaction");
                        {
                            try
                            {
                                using (cmd = new SqlCommand("Sp_ADDGSTIN", con, trans))
                                {
                                    cmd.CommandType = CommandType.StoredProcedure;
                                    cmd.Parameters.AddWithValue("@CustomerNo", Request.QueryString["cu"].ToString());
                                    
                                    cmd.Parameters.AddWithValue("@GSTINLink", "/GSTIN/" + fp);
                                    Id = Convert.ToInt32(cmd.ExecuteScalar());
                                }
    
                                trans.Commit();
                                con.Close();
                                
                                
                            }
                            catch (Exception ex)
                            {
                                trans.Rollback();
                            }
                        }
                    }
    

    fp value should be - fp = "G_" + "_"  + ID + fp.FileName.ToString(); 

    Thanks

    Tuesday, July 3, 2018 6:42 AM

All replies

  • User-369506445 posted

    hi

    you can define a default character such as @ or # or...  then replace it with your ID below like :

      fp = "G_" + "_" + "@"+ fp.FileName.ToString();
    // put here code aftre Id = Convert.ToInt32(cmd.ExecuteScalar()); fp = fp.Replace("@", Id.ToString());

    Tuesday, July 3, 2018 6:51 AM
  • User-1499457942 posted

    Hi Vahid

      Actually i want to know where i should write below line . ID value i am getting thru Stored Procedure . It is Identity field

    cmd.Parameters.AddWithValue("@GSTINLink", "/GSTIN/" + fp);

    Thanks

    Tuesday, July 3, 2018 7:25 AM
  • User-369506445 posted

    id you want to set the id for <g data-gr-id="29" id="29" class="gr_ gr_29 gr-alert gr_spell gr_disable_anim_appear ContextualSpelling ins-del multiReplace">fp</g>, you must set it before addWithValue

      fp = fp.Replace("@", Id.ToString());
     cmd.Parameters.AddWithValue("@GSTINLink", "/GSTIN/" + fp);

    if you want to set it with the ExecuteScalar result, you have to it after it 

    Id = Convert.ToInt32(cmd.ExecuteScalar());
    fp = fp.Replace("@", Id.ToString());


     

    Tuesday, July 3, 2018 7:41 AM
  • User-1499457942 posted

    Hi Vhaid

      Do i need to write Update Statement again to Update fp field with ID Value . In AddWithValue fo value is without Id

    cmd.Parameters.AddWithValue("@GSTINLink", "/GSTIN/" + fp);
    Id = Convert.ToInt32(cmd.ExecuteScalar());
    fp = fp.Replace("@", Id.ToString());
     

    Thanks

     

    Tuesday, July 3, 2018 7:52 AM
  • User-369506445 posted

    here are two ways,

    1- you can update your id in the store producer, that you need to edit the <g data-gr-id="323" id="323" class="gr_ gr_323 gr-alert gr_spell ContextualSpelling ins-del">store</g> procedure.

    2-if you can't access to the <g class="gr_ gr_397 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" id="397" data-gr-id="397">store</g> procedure, you have to get the store prouder result first ( like now ) and then update it again

    Tuesday, July 3, 2018 7:57 AM
  • User-1499457942 posted

    Hi

      Below is the Stored procedure . fp value is stored in the GLink

    BEGIN
    	--	@id int output
    	Declare @Id as Int;
    	SET NOCOUNT ON;
    
        -- Insert statements for procedure here
    	Insert into [tbl_Test] ([CustomerNo],[CustomerName][GLink]) values (@CustomerNo,@CustomerName,@GLink)
    	SELECT SCOPE_IDENTITY();
    	--SET @id=SCOPE_IDENTITY()
        --RETURN  @id
    END

    Thanks

    Tuesday, July 3, 2018 8:42 AM
  • User-369506445 posted

    if your table haveIdentity Field, you can get the id after insert below <g class="gr_ gr_4 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="4" data-gr-id="4">like</g>:

    Insert into [tbl_Test] ([CustomerNo],[CustomerName][GLink]) values (@CustomerNo,@CustomerName,@GLink)
    
    SELECT @Id  = SCOPE_IDENTITY();
    
    Declare @newPath as nvarchar(400);
    
    SELECT @newPath= REPLACE(@GLink, '@', @Id); 
    
    update [tbl_Test] 
    set [GLink]=@newPath
    where [CustomerNo]=@CustomerNo

    in c# code

    fp = "G_" + "_" + "@"+ fp.FileName.ToString();
    
    trans = con.BeginTransaction("STransaction");
                        {
                            try
                            {
                                using (cmd = new SqlCommand("Sp_ADDGSTIN", con, trans))
                                {
                                    cmd.CommandType = CommandType.StoredProcedure;
                                    cmd.Parameters.AddWithValue("@CustomerNo", Request.QueryString["cu"].ToString());
                                    
                                    cmd.Parameters.AddWithValue("@GSTINLink", "/GSTIN/" + fp);
                                    Id = Convert.ToInt32(cmd.ExecuteScalar());
                                }
    
                                trans.Commit();
                                con.Close();
                                
                                
                            }
                            catch (Exception ex)
                            {
                                trans.Rollback();
                            }
                        }
                    }

    Tuesday, July 3, 2018 9:14 AM
  • User-1499457942 posted

    Hi

     How it can be done in stored procedure which i have shown

    Thanks

    Tuesday, July 3, 2018 9:22 AM
  • User-369506445 posted

    your mean was, how to return the id from store procedure to <g class="gr_ gr_72 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="72" data-gr-id="72">C# ?</g>

    if yes please try below code :

    Insert into [tbl_Test] ([CustomerNo],[CustomerName][GLink]) values (@CustomerNo,@CustomerName,@GLink)
    
    SELECT @Id  = SCOPE_IDENTITY();
    
    Declare @newPath as nvarchar(400);
    
    SELECT @newPath= REPLACE(@GLink, '@', @Id); 
    
    update [tbl_Test] 
    set [GLink]=@newPath
    where [CustomerNo]=@CustomerNo
    ;
    
    select @Id;

    Tuesday, July 3, 2018 9:29 AM
  • User-1499457942 posted

    Hi

      I want GLink value to be updated in Stored Procedure only with Identity Id. I don't want to write extra Update Statement

    Thanks

    Tuesday, July 3, 2018 9:53 AM
  • User-369506445 posted

    in the last sample, it did for you,

    First, you have to insert row

    Insert into [tbl_Test] ([CustomerNo],[CustomerName][GLink]) values (@CustomerNo,@CustomerName,@GLink)
    
    SELECT @Id  = SCOPE_IDENTITY();
    
    Declare @newPath as nvarchar(400);
    SELECT @newPath= REPLACE(@GLink, '@', @Id); 

    then based on your Insert identity, you have to update your GLINK value

    update [tbl_Test] 
    set [GLink]=@newPath
    where [CustomerNo]=@CustomerNo
    ;

    Tuesday, July 3, 2018 10:01 AM