Have the user save the form online [closed]

0

I have an Access Control to an Application in C# (WindowsForm) where the stored procedure of Login loads me the data well.

The problem comes when I try to generate an income (in another Form) and in my income table in SQL Server I have a Column IDUSUARIO , this means that the user logged in to the Application.

I have searched on many sides and I can not find the solution or an idea of structure to have the ID of the User who started the session and thus have a registry of the Users that make Income and Output of Products.

    
asked by Juan Pablo Torres Bustamante 28.06.2018 в 11:00
source

2 answers

1

Putting hands together I found the solution, it was simpler than I thought. In case any user encounters the same problem, I indicate the solution.

in Program.cs I create a variable public static String idusuario; making reference to the Login Form. in the following way:

DataTable Datos = CAPANEGOCIO.CNUsuario.Login(this.txtUsuario.Text, this.txtContraseña.Text);

Program.idusuario = Datos.Rows[0][0].ToString();

Well that would be.

The willingness to help is appreciated.

A hug!

    
answered by 28.06.2018 в 13:19
-1

The previous answer is how you should save the values that affect the whole app: using a public property in the Program class. but since these are user variables for a whole desktop application I would create a structure or a dictionary to store all the session variables within a single object, this will help you learn how to use the session variables in an environment of web programming.

Dictionaries

in the Program class

  Public Dictionary Session <string,dynamic > = new Dictionary<string,dynamic>()

From anywhere in your app

 Program.Session["ID"] =  TuUsuario;

I use the dynamic type because in it you can store any type of data, be it string, int, etc. although you must be careful when reading it because the conversion is implicit so you can face that your data types are not compatible.

If you do not want the implicit conversion you can go directly by object, but you will have to cast all the variables when you want to invoke them.

 Public Dictionary Session<string,object> = new Dictionary<string,object>()

If you are not interested in keeping the original data type, you can use just a string dictionary

  Public Dictionary Session <string,string> = new Dictionary<string,string>()

The problem of the latter will be the conversion of complex data types such as hours or bytes.

Advantages: you do not need to declare or instantiate any other object, only

 Program.Session["<<Nombre de llave>>"] = valor

to create a new stored variable, if the key name already exists it will be updated.

on the other side are the structs

the structs are easier to manage because they limit their use to only those that are defined in the structure, in this way you will not have problems in knowing if your data is correct, they are easier to serialize and very efficient with LINQ for populate them

 public static struct Session
 {
        public int IDUsuario;
        public string NombreUsuario; 
 }

 Program.Session.IDUsuario=5;
 Program.Session.NombreUsuario="Yo";
    
answered by 29.06.2018 в 19:11