Rick Roy
Rick Roy

Reputation: 1728

Display data from a table on a C# winform application

I need some help displaying data from a table onto the winform. Normally this could be done using the datagridview but to my understanding datagridvide either displays the whole content of the table or I can use a filter function to display data that matches the filter.

The issue that I am currently facing is, I have a table called book_issued which contains the details of all book issued to students, each student has an unique regid.

I have a login form which gets the value of the regid of a student when he logs in, and then directs them to another form which is supposed to display a list of all the books the student has been issued.

To achieve that I am running the sql query

"SELECT * FROM book_issued WHERE regid = @regid"

I am skipping the process of passing the parametarized value and reading the data from the datareader.

My problem is displaying this data on the form, some help would be greatly appreciated

Stupid code that has nothing to do with the problem but ppl would vote down the question without it

 private void student_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'libDataSet8.issue' table. You can move, or remove it, as needed.
            this.issueTableAdapter1.Fill(this.libDataSet8.issue);
            // TODO: This line of code loads data into the 'libDataSet7.issue' table. You can move, or remove it, as needed.
            //this.issueTableAdapter.Fill(this.libDataSet7.issue);
            //// TODO: This line of code loads data into the 'libDataSet6.books' table. You can move, or remove it, as needed.
            //this.booksTableAdapter.Fill(this.libDataSet6.books);

        }

        private void nameToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.booksTableAdapter.name(this.libDataSet6.books, nameToolStripTextBox.Text);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }

        //Get Student Details
        private void student_details(string input)
        {
            query = "SELECT * FROM student WHERE regid = @regid";
            string conString = Properties.Settings.Default.libConnectionString;
            using (SqlCeConnection conn = new SqlCeConnection(conString))
            {
                conn.Open();
                using (SqlCeCommand cmd = new SqlCeCommand(query, conn))
                {
                    cmd.Parameters.AddWithValue("@regid", input);
                    cmd.ExecuteNonQuery();

                    SqlCeDataReader reader = cmd.ExecuteReader();

                    while (reader.Read())
                    {
                        dob = reader["dob"].ToString();
                        sbook = Convert.ToInt32(reader["book"].ToString());
                        fine = Convert.ToDouble(reader["fine"].ToString());
                    }
                    string temp = dob.Substring(5, 4);
                    int years = DateTime.Now.Year - Convert.ToInt32(temp);

                    sage = years;

                }
            }

        }
        //Method to validate user inputs
        private bool ValidateISBNi()
        {
            bool bStatus = true;
            if (isbni.Text == string.Empty)
            {
                errorProvider2.SetError(isbni, "");
                errorProvider1.SetError(isbni, "Please Enter ISBN code");
                bStatus = false;
            }
            else
            {
                 query = "SELECT * FROM books WHERE isbn = @isbn";
                 string conString = Properties.Settings.Default.libConnectionString;
                 using (SqlCeConnection conn = new SqlCeConnection(conString))
                 {
                     conn.Open();
                     using (SqlCeCommand cmd = new SqlCeCommand(query, conn))
                     {
                         cmd.Parameters.AddWithValue("@isbn", isbni.Text);
                         cmd.ExecuteNonQuery();

                         SqlCeDataReader reader = cmd.ExecuteReader();

                         while (reader.Read())
                         {
                             counter = 1;
                         }

                     }
                 }
                 if (counter == 1)
                 {
                     errorProvider1.SetError(isbni, "");
                     errorProvider2.SetError(isbni, "Good");
                     counter = 0;
                 }
                 else
                 {
                     errorProvider2.SetError(isbni, "");
                     errorProvider1.SetError(isbni, "Invalid ISBN code");
                     bStatus = false;
                 }
            }
            return bStatus;            
        }

        private bool ValidateISBNr()
        {
            bool bStatus = true;
            if (isbni.Text == string.Empty)
            {
                errorProvider2.SetError(isbnr, "");
                errorProvider1.SetError(isbnr, "Please Enter ISBN code");
                bStatus = false;
            }
            else
            {
                query = "SELECT * FROM issue WHERE isbn = @isbn AND rgid = @rgid";
                string conString = Properties.Settings.Default.libConnectionString;
                using (SqlCeConnection conn = new SqlCeConnection(conString))
                {
                    conn.Open();
                    using (SqlCeCommand cmd = new SqlCeCommand(query, conn))
                    {
                        cmd.Parameters.AddWithValue("@isbn", isbnr.Text);
                        cmd.Parameters.AddWithValue("@rgid", Variables.regid);
                        cmd.ExecuteNonQuery();

                        SqlCeDataReader reader = cmd.ExecuteReader();

                        while (reader.Read())
                        {
                            counter = 1;
                        }

                    }
                }
                if (counter == 1)
                {
                    errorProvider1.SetError(isbnr, "");
                    errorProvider2.SetError(isbnr, "Good");
                    counter = 0;
                }
                else
                {
                    errorProvider2.SetError(isbni, "");
                    errorProvider1.SetError(isbni, "Invalid ISBN code");
                    bStatus = false;
                }
            }
            return bStatus;            
        }


        private void isbni_Validating(object sender, CancelEventArgs e)
        {
            ValidateISBNi();
        }

        private void isbnr_Validating(object sender, CancelEventArgs e)
        {
            ValidateISBNr();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            bool vissue;
            vissue = ValidateISBNi();
            if (vissue)
            {

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }


    }
}

There added a bunch of random codes does that help ne1? I didnt think it would that was why i didnt post it in the first place

Upvotes: 0

Views: 4684

Answers (1)

Akemi Chou
Akemi Chou

Reputation: 64

Is this what you mean?

In your Form 1, Let's say you have a TextBox name txtRegStud, then the value from form1 will be passed to form2? You are pertaining to inheritance.

in form1 this would probably the code:

form2 f = new frm2();
f.strRegStud = txtRegStud.Text;
f.Show();
this.Dispose();

then in your form2, you need to declare public string in order to carry a value from any other forms:

public string strRegStud;
private void frm2_Load(object sender, EventArgs e)
    {
        try
        {
            OleDbConnection Con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\dbname.mdb;");
            OleDbCommand command = new OleDbCommand();
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            DataTable dt = new DataTable();
            Con.Open();
            command.CommandText = String.Format("SELECT * FROM book_issued WHERE regid ='{0}'", strRegStud);;
            command.Connection = Con;
            adapter.SelectCommand = command;
            adapter.Fill(dt);
            Con.Close();
            Con.Dispose();
            datagridview1.DataSource = dt;

        }
        catch (Exception ex)
        {
            XtraMessageBox.Show(ex.Message);
        }
    }

is this what you are looking?

Upvotes: 1

Related Questions