简体   繁体   中英

Database not accessed and no output in c#

The first 2 are function in a class and second is a code.Here i have one text box ie txtlicense and combobox cboaccident type and combobox consists 2 selecting item ie Death and Major.Now if i input licensenumber in txtlicense and select Death option from combobox which are present in data base it shows the ouput as per required , but problem is that if i input the licensenumber in textbox and select major option from combobox which are already present in database and press the button it didn't show any ouptput for major option.

public DataTable Checkdeathaccident(string LicenseNumber, string PhysicalStatus)
{
    SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB; Integrated Security=True; Initial Catalog=tprojectDB;");
    string sql = "select DeathNumber,ReportNumber,Date from tblAccident  where LicenseNumber=@LicenseNumber and PhysicalStatus=@PhysicalStatus";
    SqlCommand cmd = new SqlCommand(sql, con);
    cmd.Parameters.AddWithValue("@LicenseNumber", LicenseNumber);
    cmd.Parameters.AddWithValue("@PhysicalStatus", PhysicalStatus);

    SqlDataAdapter da = new SqlDataAdapter(cmd);

    DataTable dan = new DataTable();
    da.Fill(dan);
    return dan;
}

public DataTable Checkmajoraccident(string LicenseNumber, string PhysicalStatus)
{
    SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB; Integrated Security=True; Initial Catalog=tprojectDB;");
    string sql = "select DeathNumber,ReportNumber,Date from tblAccident where LicenseNumber=@LicenseNumber and PhysicalStatus=@PhysicalStatus";
    SqlCommand cmd = new SqlCommand(sql, con);
    cmd.Parameters.AddWithValue("@LicenseNumber", LicenseNumber);
    cmd.Parameters.AddWithValue("@PhysicalStatus", PhysicalStatus);

    SqlDataAdapter da = new SqlDataAdapter(cmd);

    DataTable dl = new DataTable();
    da.Fill(dl);
    return dl;
}    

private void button10_Click_1(object sender, EventArgs e)
{
    DataTable dan = pac.Checkdeathaccident(txtlicense.Text, cboaccidentype.Text);
    if (dan.Rows.Count > 0)
    {
        if (cboaccidentype.Text == "Death")
        {
            dataGridView2.DataSource = dan;

        }
        else
        {
           DataTable dl = pac.Checkmajoraccident(txtlicense.Text, cboaccidentype.Text);
            if (dl.Rows.Count > 0)
            {
                if (cboaccidentype.Text == "Major")
                {

                    dataGridView2.DataSource = dl;

                }                               
            }

        }
    }
    else
    {
        MessageBox.Show("No Record Found");
    }
}

As Paul mentioned you have the same functions running to fill the same gridview with the same select query, you could refactor to this and it should work, I didn't test this just an idea.

public DataTable checkAccident(string LicenseNumber, string PhysicalStatus)
{
    SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB; Integrated Security=True; Initial Catalog=tprojectDB;");
    string sql = "select DeathNumber,ReportNumber,Date from tblAccident where LicenseNumber=@LicenseNumber and PhysicalStatus=@PhysicalStatus";
    SqlCommand cmd = new SqlCommand(sql, con);
    cmd.Parameters.AddWithValue("@LicenseNumber", LicenseNumber);
    cmd.Parameters.AddWithValue("@PhysicalStatus", PhysicalStatus);

    SqlDataAdapter da = new SqlDataAdapter(cmd);

    DataTable dl = new DataTable();
    da.Fill(dl);
    return dl;
}    

private void button10_Click_1(object sender, EventArgs e)
{
    DataTable dan = pac.checkAccident(txtlicense.Text, cboaccidentype.Text);
    if (dan.Rows.Count > 0)
    {
        dataGridView2.DataSource = dan;
    }
    else
    {
        MessageBox.Show("No Record Found");
    }
}

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