3
Answers

Transaction no

Photo of Marvin kakuru

Marvin kakuru

13y
1.1k
1

Transaction no

Hi,

I am trying to generate a transaction number for every transaction I enter in my database, so I

Use the following code to select the last number to which I plan to add a "ONE"

qlConnection cn = new SqlConnection("Data Source=KATOTO-PC;Initial Catalog =thyfarm;User ID=sa;Password=Kyozi");

            string sql = ("select MAX(transno) from prodtntransno");

 

            SqlCommand cmd = new SqlCommand(sql, cn);

            cn.Open();

 

            string s1 = cmd.ExecuteScalar().ToString();

            textBox4.Text = s1.ToString();

 

This code will retrieve the last transaction number for example 11111112 to which I will add a "1"

Generating the next number which should be 11111113. Now this is where my problem begins, below the above code I have placed the code below

int a = System.Convert.ToInt32(textBox4.Text);

            int b = System.Convert.ToInt32(textBox5.Text);

            int c = (a + b);

 

            textBox3.Text = c.ToString();

 

this code is intended to add a "1" which is set as the text of textbox5.text, however when I run I get an ERROR!! "Overflow Exception was unhandled – Value was either too large or too small for an int32 "

any help will be highly appreciated.


Answers (3)

0
Photo of Vulpes
NA 98.3k 1.5m 13y
I can only think that the transaction numbers may be ouside the range of an Int32 which is (roughly) plus or minus 2 billion.

I'd try using Int64 instead which has a much larger range:

long a = System.Convert.ToInt64(textBox4.Text);
long b = System.Convert.ToInt64(textBox5.Text);
long c = (a + b); 
textBox3.Text = c.ToString();
Accepted
0
Photo of Marvin kakuru
NA 224 113.4k 13y
thanks again vulpes
0
Photo of Satyapriya Nayak
NA 53k 8m 13y
Hi Marvin,


Try this...

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
    string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    string str;
    SqlCommand com;
    protected void btn_insert_Click(object sender, EventArgs e)
    {
        Int32 a = System.Convert.ToInt32(t1.Text);
        Int32 b = System.Convert.ToInt32(t2.Text);
        Int32 c = (a + b);
        t3.Text = c.ToString();
      
    }
    protected void Page_Load(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection(strConnString);
        str = "select MAX(transno) from prodtntransno";
        com = new SqlCommand(str, con);
        con.Open();
        com = new SqlCommand(str, con);
        t1.Text = com.ExecuteScalar().ToString();
        con.Close();
    }
}



Thanks