You must Sign In to post a response.
  • Category: ASP.NET

    How to increment value and insert that value into database

    Query as follows

    i have one table as follows control_file

    ctrl_type ctrl_last_no
    PKGNO 52

    I have another table as follows Co_Package_Master

    cpm_pkg_id pkg_name
    PKG00000 EFA+PSSR

    First Query as follows
    Update control_file set ctrl_last_no = ctrl_last_no + 1 where ctrl_type = 'PKGNO'

    Second Query as follows
    select * from CONTROL_FILE where ctrl_type = 'PKGNO'

    i get the ctrl_last_nor from control file table and increment one value.

    i have Co_package_master table in that i want to insert the cpm_pkg_id as follows

    The above 53 value has get from control file and increment one value and want to be store in the co_package_master table.

    for that how can i do in using c#.
  • #762319

    string prefix="PKG"
    string s = "0000053";
    int num = Convert.ToInt32(s);
    num += 1;

    string str = num.ToString("D7");//now str value is
    PKG0000053,0000054,0000056.... till your for loop
    condition gets false.

    string Result= prefix+str;
    // Result is PKG0000053,PKG0000054.. so on

    Sridhar Thota.

    Sridhar Thota.
    Editor: DNS Forum.

  • #762320

    You can try this Query.

    When you post the question depends on sql means first share your table script if you able post data also thats easy to solve and quick for your question.

    You can try this code in C# CODE

    DataTable dts = new DataTable();
    SqlDataAdapter sqladps = new SqlDataAdapter("Select top 1 ctrl_last_no from control_file order by ctrl_last_no desc", con);

    SqlCommand sqlcmd = new sqlcmd("Update Co_package_master set cpm_pkg_id= cpm_pkg_id +cast(" + Convert.ToInt32(dts.DefaultView[0][0]) + " as varchar(20)) where cpm_pkg_id='PKG00000'", con);

    Name : Dotnet Developer-2015

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #762321

    If you need sql side means try this Query

    Create table control_file
    ctrl_type varchar(30),
    ctrl_last_no int

    Insert into control_file values('PKGNO',52)
    Insert into control_file values('PKGN1',58)

    Create table Co_Package_Master
    cpm_pkg_id varchar(30),
    pkg_name varchar(30)

    Insert into Co_Package_Master values('PKG00000','EFA+PSSR')

    Select top 1 ctrl_last_no from control_file order by ctrl_last_no desc

    Update Co_package_master set cpm_pkg_id= cpm_pkg_id +cast(53 as varchar(20)) where cpm_pkg_id='PKG00000'

    Select * from Co_Package_Master

    I have attached snapshots given below.

    Name : Dotnet Developer-2015

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

    Delete Attachment

Sign In to post your comments