Friday, February 4, 2011

Get next number from Table to in a label or in variable C# -cmd.ExecuteNonQuery

get next number from  in a label or in variable c#

Create proc usrNN
@nn int =1 output
as
insert into dbo.TblNN (NNTXT)
select 'UserAccess'
select @nn=max(nnid) from dbo.TblNN


protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            NNuser();
        }
    }

void NNuser()
    {
      
        SqlConnection myConnection = new SqlConnection(ConnectionString);
        string strSQL;
        strSQL = "usrNN";
        SqlCommand cmd = new SqlCommand(strSQL, myConnection);
        cmd.CommandType = CommandType.StoredProcedure;

        SqlParameter parm3 = new SqlParameter("@nn", SqlDbType.Int);
        parm3.Direction = ParameterDirection.Output;
        cmd.Parameters.Add(parm3);

        myConnection.Open();
        cmd.ExecuteNonQuery();
        myConnection.Close();
       // int vat = (int)ret.Value;
        //Response.Write(cmd.Parameters["@nn"].Value.ToString());
        Label1.Text = cmd.Parameters["@nn"].Value.ToString();

    }

No comments: