简体   繁体   中英

I am trying to make the text from a text box become the SQL command and then populate a datagridview with the query result

The Daily_Prices class doesn't show up as a class when I add the datatable class to populate the datagridview. I want the textbox to become the SQL command and then populate the datagridview with the query results. I can't get the Daily_Prices class to stay as a class under the data layer namespace when I add the code for the DataTable class

This is the code for the button click event to trigger the commandbox text to SQL command and to populate the datagridview with the result of the query. It says there is no class Daily_Prices in the namespace DataLayer . But when you look at my DataLayer namespace, there is a Daily_Prices class.

private void buttonGo_Click(object sender, EventArgs e)
{
    try
    {
        // Takes the commandBox text and sends it to SQL Server
        DataLayer.Commands c = new DataLayer.Commands();
        DataLayer.Daily_Prices dp = c.Command(string.Format(commandBox.Text));

        // Populates the datagridview                
        DataTable daily = DataLayer.Commands.GetDaily("Daily_Price");
        dataGridViewGetDaily.DataSource = daily;
     }
     catch { }
 }

and the namespace for DataLayer looks like this

namespace DataLayer
{
    public class Commands
    {
        public Daily_Prices Command(string commandBox)
        {
            Daily_Prices dp = new Daily_Prices();

            using (SqlConnection connect = DB.GetSqlConnection())
            {
                 using (SqlCommand cmd = connect.CreateCommand())
                 {
                     cmd.CommandText = @"{0}";
                     cmd.CommandText = string.Format(cmd.CommandText, commandBox.ToString());

                     SqlDataReader reader = cmd.ExecuteReader();

                     if (reader.Read())
                     {
                         dp.Load(reader);
                     }

                     return dp;
                 }
            }
        }

        public static DataTable GetDaily(string commandBox)
        {
            DataTable table = new DataTable("Daily_Price");
            SqlDataAdapter da = null;

            using (SqlConnection conn = DB.GetSqlConnection())
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = @"{0}";
                cmd.CommandText = string.Format(cmd.CommandText, commandBox.ToString());

                da = new SqlDataAdapter(cmd);
                da.Fill(table);
            }

            return table;
        }
    }
}

I can't find any definition of a Daily_Prices class in your DataLayer -namespace!
Maybe you missunderstand the line Daily_Prices dp = new Daily_Prices(); (which is just an object definition)?

The Only class-definition I can see in your DataLayer -namespace is Commands

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM