Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Main aim is Generate One Code From Two Words.

now i want to get list of items in globally declared list in my method with that list i compare input string.plz help me

no i facing when ever i pass my string from view list should be empty...

What I have tried:

my method
  public ActionResult Generateid(string Fname)
        {
            list = new List<string>();
            string s1 = Fname;
            string s2 = "mpm";
            string x = s1.Substring(0, 1);
            string y = s2.Substring(0, 1);
            string z = s1.Substring(1, 1);
            string c = x + y + z;
            string s = "00";
            int number = Convert.ToInt32(s);
            number += 1;
            string str1 = number.ToString("D2");
            c += str1;
                list = new List<string>();
                list.Add(c);
            Session["sring"] = c;
            return Json(c,JsonRequestBehavior.AllowGet);
        }



my class and pageload

public class Test1Controller : Controller
   {
       public static List<string> list;
       //
       // GET: /Test1/
       #region Test1
       public ActionResult Index()
       {

           return View();
       }
Posted
Updated 30-Jun-17 22:50pm

If you are trying to maintain a "global list", why are you throwing away everything in it every time you call that method, not once, but twice?

At the beginning, you discard any existing list:
C#
list = new List<string>();
string s1 = Fname;
...
Then when you have built a string to put in it, you discard it all over again:
C#
...
c += str1;
list = new List<string>();
list.Add(c);
...

TBH, it looks like you are guessing, copying code from random places without trying to understand it, and hoping it will magically all work in the end (The indentation is a big clue here, as is the code which generates the string - there is a lot of redundancy there).
That won't work: sit down, think about what you are trying to do, and design a solution instead of hoping and praying it will work. If you want a "global list", then you want to create it in one location, once only, and add and remove items as needed from there.
 
Share this answer
 
Comments
Member 13211166 1-Jul-17 4:35am    
sorry i forgot to remove that one
page load
public ActionResult Index()
{
list=new List<string>();
return View();
}
method:

public ActionResult Generateid(string Fname)
{
try
{

string s1 = Fname;
string s2 = "mpm";
string x = s1.Substring(0, 1);
string y = s2.Substring(0, 1);
string z = s1.Substring(1, 1);
string c = x + y + z;
string s = "00";
int number = Convert.ToInt32(s);
number += 1;
string str1 = number.ToString("D2");
c += str1;
list.Add(c);
Session["sring"] = list;
return Json(c, JsonRequestBehavior.AllowGet);
}
catch(Exception)
{
throw;
}
}

like this now i trying...but every time it will be list count should be 0





thank for ur responce...
girishmeena 1-Jul-17 4:39am    
Can you please explain more what are you trying to achieve?
Member 13211166 1-Jul-17 4:47am    
add my items in list
In class:
public class Test1Controller : Controller
   {
       static List<string> list;

       // GET: /Test1/
       #region Test1
       static Test1Controller() {
           list = new List<string>();
       }


Function
public ActionResult Generateid(string Fname)
       {
           try
           {
               string s1 = Fname;
               string s2 = "mpm";
               string x = s1.Substring(0, 1);
               string y = s2.Substring(0, 1);
               string z = s1.Substring(1, 1);
               string c = x + y + z;
               string s = "00";
               int number = Convert.ToInt32(s);
               number += 1;
               string str1 = number.ToString("D2");
               c += str1;
               list.Add(c);
               Session["sring"] = list;


               return Json(c, JsonRequestBehavior.AllowGet);
           }
           catch(Exception)
           {
               throw;
           }



No i am Trying like this now it working ...
thanks for all...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900