Parse the selected value from ddl and fill another ddl with information + NHibernate + C#
-
Hi, I hope someone can help with with my problem, i have searched the Internet for information but i couldnt find any examples neither the answear. I use NHibernate and c# for my asp.net site. I have the "classic" problem. I have a dropdownlist with "Regions" and another one "Municipalities". When I choose an item in the ddlRegions the appropiate municipalites should be shown in ddlMunicipalties. I cant get this to work, do anyone have a code that does this and could share it with me? I have tried for a few evenings but i cant get it to work. Thanks Thommie
-
Hi, I hope someone can help with with my problem, i have searched the Internet for information but i couldnt find any examples neither the answear. I use NHibernate and c# for my asp.net site. I have the "classic" problem. I have a dropdownlist with "Regions" and another one "Municipalities". When I choose an item in the ddlRegions the appropiate municipalites should be shown in ddlMunicipalties. I cant get this to work, do anyone have a code that does this and could share it with me? I have tried for a few evenings but i cant get it to work. Thanks Thommie
Hi Thommie, I think your looking for somting like this: private void ddlRegions_SelectedIndexChanged(object sender, System.EventArgs e) { RetrieveMunicipalities( int.Parse( ddlRegions.SelectedValue ) ); } private void RetrieveMunicipalities( int region) { ddlMunicipalities.DataSource = Retrieve_Municipalities( region); ddlMunicipalities.DataBind(); } Greetins, Marco van der Linden
-
Hi Thommie, I think your looking for somting like this: private void ddlRegions_SelectedIndexChanged(object sender, System.EventArgs e) { RetrieveMunicipalities( int.Parse( ddlRegions.SelectedValue ) ); } private void RetrieveMunicipalities( int region) { ddlMunicipalities.DataSource = Retrieve_Municipalities( region); ddlMunicipalities.DataBind(); } Greetins, Marco van der Linden
-
Hi Marco, Whats "= Retrieve_Municipalities( region)"? is that a method to bind the data from the database? can you please show me? Thanks for the answear Thommie
Hi Thommie, Its a method that retrieve data from the database. public DataTable Retrieve_Municipalities( int region) { string connectieString = ConfigurationSettings.AppSettings["ConnectieString"]; SqlConnection connectie = new SqlConnection(connectieString); SqlCommand commando = new SqlCommand("spRetrieveMunicipalities", connectie); SqlDataAdapter adapter = new SqlDataAdapter(commando); commando.CommandType = CommandType.StoredProcedure; SqlParameter parameterHoofdgroep = new SqlParameter("@region", SqlDbType.Int); parameterregion.Value = region; commando.Parameters.Add(parameterregion); DataTable regions= new DataTable(); adapter.Fill(regions); connectie.Close(); return regions; } Greetings, Marco :doh:
-
Hi Thommie, Its a method that retrieve data from the database. public DataTable Retrieve_Municipalities( int region) { string connectieString = ConfigurationSettings.AppSettings["ConnectieString"]; SqlConnection connectie = new SqlConnection(connectieString); SqlCommand commando = new SqlCommand("spRetrieveMunicipalities", connectie); SqlDataAdapter adapter = new SqlDataAdapter(commando); commando.CommandType = CommandType.StoredProcedure; SqlParameter parameterHoofdgroep = new SqlParameter("@region", SqlDbType.Int); parameterregion.Value = region; commando.Parameters.Add(parameterregion); DataTable regions= new DataTable(); adapter.Fill(regions); connectie.Close(); return regions; } Greetings, Marco :doh:
Hi, My code looks like this and it doesnt work: This is with NHibernate. The error code : fetchQ.ParamName = "Region.Id"; The error text: NHibernate.QueryException: Type mismatch in NHibernate.Expression.EqExpression: Region.Id expected type System.Int32, actual type System.String RegionId in database is int. And the hbm file is mapped Int32
private void BindMunicipalities(int regionId) { try { string municipalityHql = "SELECT count(municipality.Id) from Municipality municipality WHERE municipality.Region.Id = :regionId"; IList queries = new List(); Query q = new Query(); q.ParamInt = regionId; q.ParamName = "regionId"; queries.Add(q); int municipalityCount = DataManagement.CoreRepository.RunCountStatement(municipalityHql, queries); if (municipalityCount > 0) { IList fetchQueries = new List(); Query fetchQ = new Query(); fetchQ.ParamName = "region.Id"; fetchQ.ParamInt = regionId; fetchQ.ParamEvaluationType = EvaluationType.Equals; fetchQ.ParamString = string.Empty; fetchQueries.Add(fetchQ); IList municipalities = DataManagement.CoreRepository.GetAll(fetchQueries, 0, 0, true, "Name"); ddlMunicipality.DataValueField = "Id"; ddlMunicipality.DataTextField = "Name"; ddlMunicipality.DataSource = municipalities; ddlMunicipality.DataBind(); } } catch (Exception ex) { logger.Error("Failed to load municipalities.", ex); this.lblError.Visible = true; } } protected void ddlRegions_SelectedIndexChanged(object sender, EventArgs e) { this.BindMunicipalities(int.Parse(this.ddlRegions.SelectedValue)); }
modified on Thursday, March 20, 2008 5:16 PM