Click here to Skip to main content
15,891,895 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.DataSource = GetData();
            DropDownList1.DataValueField = "Branch";
            DropDownList1.DataTextField = "Branch";
            DropDownList1.DataBind();
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        using (SqlConnection Cn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=F:\AAAAAAAAAAA\FINGERPRINT\APP_DATA\QUESTIONS.MDF;Integrated Security=True;")) 
        {
            using (SqlCommand Cmd = new SqlCommand("select * from questions where Branch= @Branch"))
            {
                Cn.Open();

                Cmd.Parameters.AddWithValue("@Branch", int.Parse(DropDownList1.SelectedValue));
                SqlDataReader Dr = Cmd.ExecuteReader();
                if (Dr.HasRows)
                {
                    GridView1.DataSource = Dr;
                    GridView1.DataBind();
                }
                Dr.Close();

                Cn.Close();
            }

        }
    }
   DataTable GetData()
    {
        DataTable dt = new DataTable();
         SqlConnection Con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=F:\AAAAAAAAAAA\FINGERPRINT\APP_DATA\QUESTIONS.MDF;Integrated Security=True;");
        {
             SqlCommand Cmd = new SqlCommand("SELECT * FROM  questions ");
            
                Con.Open();
                SqlDataAdapter adpt = new SqlDataAdapter(Cmd);
                adpt.Fill(dt);
            }

        }
    }


What I have tried:

i have try add protected but get same error in data table getdata()
Posted
Updated 9-Apr-17 0:11am
Comments
Tomas Takac 9-Apr-17 5:58am    
What error?

In GetData() method you are creating the data table but never returning it so I assume your compilation error is "not all paths are returning a value". It should look like this:
C#
DataTable GetData()
{
    DataTable dt = new DataTable();
    using(SqlConnection Con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=F:\AAAAAAAAAAA\FINGERPRINT\APP_DATA\QUESTIONS.MDF;Integrated Security=True;"))
    using(SqlCommand Cmd = new SqlCommand("SELECT * FROM  questions ", Con))
    {		
    	Con.Open();
    	SqlDataAdapter adpt = new SqlDataAdapter(Cmd);
    	adpt.Fill(dt);
    }
    return dt; // <-- this is what you were missing
}

Please note I added using statements as both SqlConnectin and SqlCommand implement IDisposable.
 
Share this answer
 
v2
Comments
Member 13045876 10-Apr-17 11:48am    
there is and error of Fill: SelectCommand.Connection property has not been initialized.
Tomas Takac 10-Apr-17 14:59pm    
True, I updated the code.
You declared GetData as returning a DataTable, but the method does not return anything.
Add this line at the end of the method:
C#
return dt;


BTW: don't hardcode connection strings: read them from a configuration file!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900