1

Bit of a hobbyist coder (and Novice) so I appreciate I may be approaching this in the wrong way so any advice on changing my approach would also be appreciated.

The idea is to have a "More Details" button on a form, that will open another form showing the customers contact details retrieved from a SQL table. The table called 'Project_Info' contains all of the contact/address data, using a 'Project' ID as the unique identifier.

The code for the button:

private void button2_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("Data Source=WIN-M9TBGRD70BU;Initial Catalog=Disk_Tracker;User ID=Tracker;Password=********");
    SqlDataAdapter sda = new SqlDataAdapter("Select * FROM Project_Info WHERE (Project = 'P3890T')", con);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    {
        this.Hide();
        TRACK_10_PI T10 = new TRACK_10_PI();
        T10.Show();
    }
}

The new form TRACK_10_PI opens but always displays the data from the top row rather than as the WHERE clause has specified.

The reason I want the data retrieved from a database rather than simply hard coding it to the page is to accommodate possible changes to the information.

Thanks in advance

5
  • Could the problem be in TRACK_10_PI? Commented May 18, 2018 at 12:34
  • What results do you get when you run the query directly against the database using your favourite database querying tool? Commented May 18, 2018 at 12:39
  • where you are consuming data filled in dt.? Commented May 18, 2018 at 12:41
  • it would appear that TRACK_10_PI has it's own query that doesn't include a WHERE clause. I don't see where you're using the datatable that you populate here. Commented May 18, 2018 at 12:53
  • where are you executing the query? Commented May 21, 2018 at 11:20

2 Answers 2

1

I would suggest you will use usinngs, so you don't Need to dispose expliciet. Place a datagridview on your form which is filled by your code. You can parameterize your instantiation of your form. Now you have to fill the datasouse in TRACK_10_PI.

Example:

using (SqlConnection con = new SqlConnection("Data Source=WIN-M9TBGRD70BU;Initial Catalog=Disk_Tracker;User ID=Tracker;Password=********"))
{
    using (SqlDataAdapter sda = new SqlDataAdapter("Select * FROM Project_Info WHERE (Project = 'P3890T')", con))
    {
        DataTable dt = new DataTable();
        sda.Fill(dt);
        this.Hide();
        TRACK_10_PI T10 = new TRACK_10_PI(sda);
        T10.Show();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I'm suspecting you're not passing the newly created dt in the form TRACK_10_PI. I don't see any problem with you code except that you should dispose the SqlConnection and the SqlDataAdapter objects.

Comments

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.