Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I can not pass one value from one class to another, when i try, the value pass to 0 instead the value from the first class.

I have this: an method that will check if the person is in the database, and the variable IDautor will gain the id is on the database, it will enter in the if clauses then the array id2 in the position 1 will save the value from IDautor... I pretend to use that value in the class Artigo, but the value came always 0 and i don't no why Class Autor

    public bool Login(TextBox txtUser, TextBox txtPassword)

    {

        string utl, pass;

        try
        {
            utl = txtUser.Text;
            pass = txtPassword.Text;

            _Sql = "Select Count(IDAutor) From Autor Where uUser = @uUser and Pass = @Pass";

            SqlCommand cmd = new SqlCommand(_Sql, Conn);

            cmd.Parameters.Add("@uUser", SqlDbType.VarChar).Value = utl;
            cmd.Parameters.Add("@Pass", SqlDbType.VarChar).Value = pass;

            Conn.Open();

            IDautor = (int)cmd.ExecuteScalar();                
            if (IDautor > 0)
            {
                MessageBox.Show("Bem Vindo");                        
                Conn.Close();
                id2[1] = IDautor;
                return true;
            }
            else
            {
                MessageBox.Show("Tera que tentar de novo");
                Conn.Close();
                return false;
            }                
        }
        catch(Exception ex)
        {
            MessageBox.Show("ERRO Message: "+ ex);
            Conn.Close();
            return false;
        }
    }



public int[] id2 
    {
        get { return this.id2; }
    }

and in trying to save the value in array id2 and use that value in another class

Class Artigo

public string AddArtigo(string newArtigo)
   {
       if (newArtigo == null)
       {
           return "Nao selecionou o PDF desejado. Tente de novo";               
       }
       if (newArtigo != null)
       {
           using (FileStream st = new FileStream(newArtigo, FileMode.Open, FileAccess.Read))
           {
               SqlConnection Conn = new SqlConnection(@"Data Source=.SQLEXPRESS;AttachDbFilename=C:UsersConvidado1DesktopAplica?ao C#Aplica?aoITArtiConfITArtiConfDatabase1.mdf;Integrated Security=True;User Instance=True");

               Autor au = new Autor();

               byte[] buffer = new byte[st.Length];
               st.Read(buffer, 0, (int)st.Length);
               st.Close();

               Conn.Open();
               SqlCommand cmd = Conn.CreateCommand();
               cmd.CommandText = "insert into Artigo (idAutor) values('" + au.id2[1] + "')";
               cmd.ExecuteNonQuery();
               Conn.Close();


               SqlCommand cmd1 = new SqlCommand("UPDATE SomeTable SET Artigo=@Artigo WHERE idAutor =" + au.id2[1], Conn);
               cmd1.Parameters.AddWithValue("@Artigo", buffer);
               Conn.Open();
               int i = cmd1.ExecuteNonQuery();
               Conn.Close();                   
           }
           return "Guardado";
       }

       return "Error";           
   }

the au.id2[1] become 0 instead the value on the other class.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
151 views
Welcome To Ask or Share your Answers For Others

1 Answer

Autor au = new Autor();

This line of code creates new Author object, which has all properties set into their default values. You don't call anything more on that object before trying to get id value, so it's value is 0.

You should consider making that property static, what will makes it's value persistent.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...