0

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

2
  • if you use a databound DGV it is up to your select to a) decide how many rows are tranferred into the dataset and b) which columns are sent and created in the DGV. But you can also create a navigation control and copy some bound data to various controls like labels, checkboxes textboxes etc.. Commented Jul 18, 2014 at 17:39
  • Sometimes this happens. It often means they believe the question is of little use to others in the future. Think nothing of it - or not too much! Commented Jul 18, 2014 at 17:49

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

1 Comment

This would do, I was able to get to pass the regid from form1 to form2 but was at a loss on how to diplay the data thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.