Create TxnNumber based on VoucherType Year and Month using c#

Last Reply 9 days ago By dharmendr

Posted 14 days ago

Code behind - 

 

private string CashDeposit_TxnNumber(string connection, string query)
        {
            string CSHD_NewTxnNumber = string.Empty;
            string GetYearLastTwoDigit = string.Empty;
            string GetMonthTwoDigit = string.Empty;
            using (SqlConnection cnn = new SqlConnection(connection))
            {
                cnn.Open();
                SqlCommand cmd = new SqlCommand(query, cnn);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    string i = dr[0].ToString();
                    if (string.IsNullOrEmpty(i))
                    {
                        CSHD_NewTxnNumber = "000001";
                    }
                    else
                    {
                        i = i.Substring(1);
                        int j = Convert.ToInt32(i);
                        j = j + 1;
                        GetYearLastTwoDigit = dateTimePicker1.Value.ToString("yy");
                        GetMonthTwoDigit = dateTimePicker1.Value.ToString("MM");
                        CSHD_NewTxnNumber = "GP" + GetYearLastTwoDigit + GetMonthTwoDigit + "CSHD" + j.ToString().PadLeft(6, '0');
                    }
                }
                cnn.Close();
            }
            return string.Concat(CSHD_NewTxnNumber);
        }

 

private void button1_Click(object sender, EventArgs e)
        {
            string GetYearLastTwoDigit = string.Empty;
            string GetMonthTwoDigit = string.Empty;
            GetYearLastTwoDigit = dateTimePicker1.Value.ToString("yy");
            GetMonthTwoDigit = dateTimePicker1.Value.ToString("MM");
            string PreviousId_ForTxnNumber = "Select MAX(TxnNumber) From AllTypeTxn where TxnYear='" + GetYearLastTwoDigit + "' and TxnMonth='" + GetMonthTwoDigit + "' and TxnType='" + "CSHD" + "'";
            string NewTxnNumber = CashDeposit_TxnNumber(AvoidRepeatId, PreviousId_ForTxnNumber);

            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "cashDeposit";
            cmd.Parameters.AddWithValue("@BranchName", SqlDbType.VarChar).Value = comboBox1.Text.Trim();
            cmd.Parameters.AddWithValue("@BranchId", SqlDbType.VarChar).Value = textBox1.Text.Trim();
            cmd.Parameters.AddWithValue("@TxnVoucher", SqlDbType.VarChar).Value = "Cash Deposit";
            cmd.Parameters.AddWithValue("@TxnDate", SqlDbType.VarChar).Value = txtTxndate.Text.Trim();
            cmd.Parameters.AddWithValue("@TxnYear", SqlDbType.VarChar).Value = dateTimePicker1.Value.ToString("yy");
            cmd.Parameters.AddWithValue("@TxnMonth", SqlDbType.VarChar).Value = dateTimePicker1.Value.ToString("MM");
            cmd.Parameters.AddWithValue("@TxnType", SqlDbType.VarChar).Value = "CSHD";
            cmd.Parameters.AddWithValue("@TxnNumber", SqlDbType.VarChar).Value = NewTxnNumber;
            cmd.Connection = cnn;
            cnn.Open();
            cmd.ExecuteNonQuery();
            this.Close();
        }

This String give error message. Input string was not in correct format. Please help me out.

You are viewing reply posted by: dharmendr 9 days ago.
Posted 9 days ago

Please debug and check the value of i. I think you are getting null value in i. So if getting null value then before Substring check for null condition.

If i is not null then Substring the i else let as it is.