How to make Class1.Cs appear?

Asked

Viewed 72 times

-1

In my WEB FOLDER I have the file aspx.cs and the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using WebApplication1.App_Code;

namespace WebApplication1.bibliotecario
{
    public partial class adicionar_livros : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\samue\source\repos\WebApplication1\WebApplication1\App_Data\gerenciamentoBiblioteca.mdf;Integrated Security=True");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (con.State == System.Data.ConnectionState.Open)
            {
                con.Close();
            }
            con.Open();

        }

        protected void b1_Click(object sender, EventArgs e)
        {

            string books_image_name = Class1.GetRandomPassword(10);

            string path = "";
            f1.SaveAs(Request.PhysicalApplicationPath + "/bibliotecario/imagens_livros/" + f1.FileName.ToString());
            path = "imagens_livros/"+f1.FileName.ToString();
            
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "insert into livros values('"+ titulolivros.Text +"','"+ path.ToString() + "','"+ nomeautor.Text + "','" + isbn.Text + "','" + qtd.Text + "')";
            cmd.ExecuteNonQuery();

            msg.Style.Add("display", "block");
        }
    }
}
   

And I have a class .cs calling for Class1 inside an Asp.net folder, App_code folder. But every time I call Class1 she is not recognized, there is no option to put it in my code, as I do to have access to it?

inserir a descrição da imagem aqui

This is the class Class1.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for Class1
/// </summary>
public class Class1
{
    
        public static string GetRandomPassword(int length)
    {
    char[] chars = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
    string password = string.Empty;
    Random random = new Random();

    for (int i = 0; i < length; i++)
    {
    int x = random.Next(1, chars.Length);
    //For avoiding Repetation of Characters
    if (!password.Contains(chars.GetValue(x).ToString()))
    password += chars.GetValue(x);
    else
    i=i-1;
    }
    return password;
    }

}
  • lacked the using WebApplication1.App_Code. But without seeing the class Class1.cs (bad name), there is no way to guess if she is with the correct namespace and nor if you can access the method without first instantiating it

  • And include the question fact code, avoid presenting your problem through screenshots

  • Good afternoon Leandro, I edited the question and I put the code at disposal, referring to class I’ll leave it down here if you can help me

1 answer

0

Just add the namespace you want before the class in this case by your using I believe that the ideal would be something like the following by putting as namespace "Webapplication1.App_code":

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;



namespace WebApplication1.App_Code
{

/// <summary>
/// Summary description for Class1
/// </summary>
public class Class1
{
    
        public static string GetRandomPassword(int length)
    {
    char[] chars = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
    string password = string.Empty;
    Random random = new Random();

    for (int i = 0; i < length; i++)
    {
    int x = random.Next(1, chars.Length);
    //For avoiding Repetation of Characters
    if (!password.Contains(chars.GetValue(x).ToString()))
    password += chars.GetValue(x);
    else
    i=i-1;
    }
    return password;
    }

}
}
  • Thanks for the reply Lucas, but I still can not access Class1 to put in my code :(((

  • The only solution I found on youtube was not clear to me of how to do , follows here as it is written there: In Latest version of visual studio you can not access Class1 file, you can do one Thing, copy that Class1 file in same file class in the same file and then you can access it.

  • You tried to click on the class and press Ctrl+. (control + dot) because this shortcut pulls the class references.

  • Already and still does not appear, I asked on gringo stackoverflow and said I need to put using WebApplication1.App_Code;, but even putting still unable to access Class1 in my aspx.Cs file

  • This Webapplication1.library namespace is another project? if it is you will have to add the Webapplication1.App_code reference.

  • Actually it’s just an SLN file, I’m starting at C# so if I make a mistake, forgive me, I’ll send you the image of how you are https://imgur.com/a/R6Q3wsd

  • How do I add the reference if you need?

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.