Click here to Skip to main content
15,891,713 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a grid view for which i am using only one column and now what is the problem when i am trying to select into first grid view the respectively result is not showing in the second one which is also grid view plz tell me

Here's the code:
C#
[WebMethod]
      public   List<properties> GetStates()
        {
            SqlConnection oSqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
            SqlCommand oSqlCommand = new SqlCommand("select * from States", oSqlConnection);
            oSqlCommand.CommandType = CommandType.Text;
            oSqlConnection.Open();
            List<properties> oListproperties = new List<properties>();
            using (SqlDataReader OReader = oSqlCommand.ExecuteReader())
            {
                while (OReader.Read())
                {
                    properties Oproperties = new properties();
                    Oproperties.Id = Convert.ToInt32(OReader["State_ID"].ToString());
                    Oproperties.State = OReader["State"].ToString();
                    oListproperties.Add(Oproperties);
                }

            }
            return oListproperties;

        }
        [WebMethod]
      public List<properties> GetCity(int ID)
      {

          //properties Oproperties1 = new properties();
          //Oproperties1.Id = StateID;
            
            SqlConnection oSqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
      SqlCommand oSqlCommand = new SqlCommand("select * from City  where State_ID ="+ID , oSqlConnection);
            oSqlCommand.CommandType = CommandType.Text;
            oSqlConnection.Open();
          List<properties>  oListproperties=new List<properties>();
          using(SqlDataReader OReader = oSqlCommand.ExecuteReader())
          {while(OReader.Read())
          
          {properties Oproperties=new properties();
              Oproperties.Id=Convert.ToInt32(OReader["ID"].ToString());
             Oproperties.City=OReader["City"].ToString();
              oListproperties.Add(Oproperties);
          }
          return oListproperties;
          }
      
      }
Posted
Updated 25-Jul-13 19:59pm
v4
Comments
vinay.tatipamula 25-Jul-13 8:35am    
Please eloborate your question? it is not clear. or show some code?
Abhimanyu vij 26-Jul-13 1:01am    
[WebMethod]
public List<properties> GetStates()
{
SqlConnection oSqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
SqlCommand oSqlCommand = new SqlCommand("select * from States", oSqlConnection);
oSqlCommand.CommandType = CommandType.Text;
oSqlConnection.Open();
List<properties> oListproperties = new List<properties>();
using (SqlDataReader OReader = oSqlCommand.ExecuteReader())
{
while (OReader.Read())
{
properties Oproperties = new properties();
Oproperties.Id = Convert.ToInt32(OReader["State_ID"].ToString());
Oproperties.State = OReader["State"].ToString();
oListproperties.Add(Oproperties);
}

}
return oListproperties;

}
[WebMethod]
public List<properties> GetCity(int ID)
{

//properties Oproperties1 = new properties();
//Oproperties1.Id = StateID;

SqlConnection oSqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
SqlCommand oSqlCommand = new SqlCommand("select * from City where State_ID ="+ID , oSqlConnection);
oSqlCommand.CommandType = CommandType.Text;
oSqlConnection.Open();
List<properties> oListproperties=new List<properties>();
using(SqlDataReader OReader = oSqlCommand.ExecuteReader())
{while(OReader.Read())

{properties Oproperties=new properties();
Oproperties.Id=Convert.ToInt32(OReader["ID"].ToString());
Oproperties.City=OReader["City"].ToString();
oListproperties.Add(Oproperties);
}
return oListproperties;
}

}
vinay.tatipamula 25-Jul-13 8:39am    
I assumed you need Row_Command event on your First GridView where you can get the clicked row object, based on that you can fetch information what ever to bind the second GridView?
Ganesan Senthilvel 25-Jul-13 21:27pm    
Share your code for the clarity
Abhimanyu vij 26-Jul-13 1:01am    
[WebMethod]
public List<properties> GetStates()
{
SqlConnection oSqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
SqlCommand oSqlCommand = new SqlCommand("select * from States", oSqlConnection);
oSqlCommand.CommandType = CommandType.Text;
oSqlConnection.Open();
List<properties> oListproperties = new List<properties>();
using (SqlDataReader OReader = oSqlCommand.ExecuteReader())
{
while (OReader.Read())
{
properties Oproperties = new properties();
Oproperties.Id = Convert.ToInt32(OReader["State_ID"].ToString());
Oproperties.State = OReader["State"].ToString();
oListproperties.Add(Oproperties);
}

}
return oListproperties;

}
[WebMethod]
public List<properties> GetCity(int ID)
{

//properties Oproperties1 = new properties();
//Oproperties1.Id = StateID;

SqlConnection oSqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
SqlCommand oSqlCommand = new SqlCommand("select * from City where State_ID ="+ID , oSqlConnection);
oSqlCommand.CommandType = CommandType.Text;
oSqlConnection.Open();
List<properties> oListproperties=new List<properties>();
using(SqlDataReader OReader = oSqlCommand.ExecuteReader())
{while(OReader.Read())

{properties Oproperties=new properties();
Oproperties.Id=Convert.ToInt32(OReader["ID"].ToString());
Oproperties.City=OReader["City"].ToString();
oListproperties.Add(Oproperties);
}
return oListproperties;
}

}

1 solution

as follows :

C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (SqlConnection connection = new SqlConnection("server=.;uid=xxx;pwd=xxx;database=Northwind"))
            {
 
                SqlDataAdapter dataAdpater = new SqlDataAdapter("SELECT CategoryID, CategoryName,Description FROM Categories", connection);                
 
                DataSet dataSet = new DataSet();
                dataAdpater.Fill(dataSet, "Categories");
 
                if (dataSet.Tables.Contains("Categories"))
                {
                    gvMain.DataSource = dataSet.Tables["Categories"].DefaultView;
                    gvMain.DataKeyNames = new string[] { "CategoryID" };
                    gvMain.DataBind();
                }                
 
            }
        }
    }
     
    protected void gvMain_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {        
 
        using (SqlConnection connection = new SqlConnection("server=.;uid=xxx;pwd=xxx;database=Northwind"))
        {
 
            SqlDataAdapter dataAdpater = new SqlDataAdapter("SELECT * FROM Products where CategoryID=@CategoryID ", connection);
 
            dataAdpater.SelectCommand.Parameters.Add(new SqlParameter("@CategoryID", gvMain.DataKeys[e.NewSelectedIndex].Value));
 
            DataSet dataSet = new DataSet();
            dataAdpater.Fill(dataSet, "Products");
 
            if (dataSet.Tables.Contains("Products"))
            {
                gv.DataSource = dataSet.Tables["Products"].DefaultView;                
                gv.DataBind();
            }
 
        }
    }
 
Share this answer
 
v2

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