locked
Not Generating Application ID in required format RRS feed

  • Question

  • User-1236024542 posted

    I have a code to generate application id (for storing value into database), but somehow its not generating in proper format that I want.

    It suppose to be Generate like this "IRBApplication12017"

    but its generating like this "2017"

    Code:

    SqlCommand cmd;
                    SqlConnection con = new SqlConnection("Data Source=RITUL-DELL;Initial Catalog = GannonIRB; Integrated Security = True");
                    con.Open();
                    cmd = new SqlCommand("SELECT TOP 1 appid_int FROM [dbo].[tblirbapplicationform] ORDER BY appid_int DESC", con);
                    SqlDataReader dr;
                    dr = cmd.ExecuteReader();
                    string nid = "IRBApplication";
                    string newid = "";
                    while (dr.Read())
                    {
                        newid = nid + (dr.GetSqlInt32(0) + 1).ToString();
                    }
                    newid = newid + DateTime.Now.Year.ToString();
                    dr.Close();
                    con.Close();

    Friday, September 8, 2017 3:16 PM

All replies

  • User2103319870 posted

    ritul29

    It suppose to be Generate like this "IRBApplication12017"

    but its generating like this "2017"

    Most likely the issue will be ,the datareader is not returning any values. Are you getting any exception while running the code. if you havent tried it yet , you might consider wrapping your code inside a try catch block and see if you are getting any exceptions while executing your code

    Also you could try adding a check condition before reading from datareader to check if there is any values in datareader and then if there is not value add a new logic to handle that scenario. You can use SqlDataReader.HasRows Property for that

     SqlCommand cmd;
                    SqlConnection con = new SqlConnection("Data Source=RITUL-DELL;Initial Catalog = GannonIRB; Integrated Security = True");
                    con.Open();
                    cmd = new SqlCommand("SELECT TOP 1 appid_int FROM [dbo].[tblirbapplicationform] ORDER BY appid_int DESC", con);
                    SqlDataReader dr;
                    dr = cmd.ExecuteReader();
                    string nid = "IRBApplication";
                    string newid = "";
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            newid = nid + (dr.GetSqlInt32(0) + 1).ToString();
                        }
                        
                    }
                    else
                    {
                        //if control reaches here that means there is not records on datareader
                        //Add your logic to handle the scenario
                        
                    }
                    newid = newid + DateTime.Now.Year.ToString();
                    dr.Close();
                    con.Close();

    Friday, September 8, 2017 4:11 PM