Jack @ ASP.NET

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries Tagged ‘Name’

Add NHibernate Configuration in web.config, and reuse an existing connectionstring

When we use nHibernate in asp.net, most time we use a nhibernate.cfg.xml to define the database connection. And for some scenarioes, we may also have a database connection defined in web.config. So, there might be 2 definitions for the same database. Here is the way to combine the 2 definitions and in web.config.

in web.config

 1:  <configuration>
 2:    <configSections>
 3:      <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
 4:    </configSections>
 5:    <appSettings>
 6:    </appSettings>
 7:    <connectionStrings>
 8:      <add name="MyDefaultDatabase" connectionString="Data Source=......" />
 9:    </connectionStrings>
10:    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
11:      <session-factory>
12:        <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
13:        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
14:        <property name="connection.connection_string_name">MyDefaultDatabase</property>
15:        <property name="command_timeout">300</property>
16:        <property name="show_sql">false</property>
17:      </session-factory>
18:    </hibernate-configuration>
19:  ……………

 

and pay attention,

  1. The secion name must be ‘hibernate-configuration’ , or it will not work. Very tricky.
  2. The type must be ‘NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" , also very tricky.
  3. Use connection.connection_string_name if you want to reuse an existing database connection.
  4. in your code, use
  5. new NHibernate.Cfg.Configuration().Configure();
    instead of given a nhibernate.cfg.xml file parameter.

 

Now, you only have 1 database connections in your web.config~

Displaying Markers on the Chart via Microsoft Chart Controls

01 private void BindData() {
02
03 var exams = new List<Exam>()
04 {
05 new Exam() { Name = "Exam 1", Point = 10 },
06 new Exam() { Name = "Exam 2", Point = 12 },
07 new Exam() { Name = "Exam 3", Point = 15 },
08 new Exam() { Name = "Exam 4", Point = 2 }
09 };
10
11 var series = ExamsChart.Series["ExamSeries"];
12
13 foreach (var exam in exams) {
14
15 var point = new DataPoint();
16 point.SetValueXY(exam.Name, exam.Point);
17
18 point.Label = exam.Point.ToString();
19 series.Points.Add(point);
20 }
21
22 ExamsChart.DataSource = exams;
23 ExamsChart.DataBind();
24 }
Microsoft Chart

Microsoft Chart