Question

i am working on ado.net project,, i used to have separate classes for codes ,,, now i am trying to learn more about local databases but i am stock when i tried to call class that contain SQL commands ,,, please have a look to my code i commented where i stock

    namespace Maintenance
    {
        public partial class registerReports : Form
        {
            private void btnSave_Click(object sender, EventArgs e)
            {
                //the code works if it exists here
                /**
                    conn.Open();
                    com.Parameters.AddWithValue("@reportID",
                    tbReportIDox.Text);
                    com.ExecuteNonQuery();
                    conn.Close();
                **/
            }
        }
    }

    class reportTableSQL 
    {
        public void reportTable()
        {
            string connectionString = connectionString = "Data Source=
            ..//..//maintenanceDB.sdf";
            SqlCeConnection conn = new SqlCeConnection(connectionString);

            using (SqlCeCommand com = new SqlCeCommand("INSERT INTO
            ReportForm VALUES(@reportID)", conn))
            {
                // if i call this method from class registerReports : Form
                // it doesn't recognise tbReportIDox.Text as
                //it isn't exist in this class

                /**
                    conn.Open();
                    com.Parameters.AddWithValue("@reportID",
                    tbReportIDox.Text);
                    com.ExecuteNonQuery();
                    conn.Close();
                **/
            }
        }
    }

thank you

Was it helpful?

Solution

What you can do is to pull tbReportIDox.Text up as a parameters to the method, that is

public void reportTable(string reportName)
{   
    ...
    com.Parameters.AddWithValue("@reportID",    reportName);
    ...
}

And then on a button click

private void btnSave_Click(object sender, EventArgs e)
{
        // resolve instance of reportTableSQL class
        // for example through: new reportTableSQL();
        var reportGenerator = new reportTableSQL();
        reportGenerator.reportTable(tbReportIDox.Text);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top