Gridview encoding, or how to run commands before gridview's default databinding, or, how do I disabl

S

Sergei Shelukhin

I have the following problem with gridview. I use MySQL database that
has Russian characters in cp1251 encoding, and I get ????????s instead
of Russian characters on all my ASP.NET pages.
To fix this, I use helper function to get database connection, it goes:

public static OdbcConnection GetConnection()
{
OdbcConnection cn = new
OdbcConnection(ConfigurationManager.ConnectionStrings["DB"].ConnectionString);
OdbcCommand cmd = new OdbcCommand("set
character_set_client='cp1251'",cn);
cn.Open();
cmd.ExecuteNonQuery();
cmd.CommandText = "set character_set_results='cp1251'";
cmd.ExecuteNonQuery();
cmd.CommandText = "set collation_connection='cp1251_general_ci'";
cmd.ExecuteNonQuery();
return cn;
}
It works well for all my pages, components etc.

Gridview, however, uses its own means to get the connection so it still
gets ????s; to fix it, I use the following:
protected override void OnPreRender(EventArgs e)
{
if (!IsPostBack)
{
OdbcConnection cn = SEHelper.GetConnection();
cn.Close();
GridView1.DataBind();
}
base.OnPreRender(e);
}

For some reason (connection pooling? some session kept between mysql
and aspnet? odbc driver quirks?) everything works perfectly well with
this code but it's kinda ugly, so, I have the following questions:
1) Is there any way to change connection used to get data into
SqlDataSource before it binds to data?
2) If there's none, is there any way to run commands against the
connection it uses before it binds?
3) If that's not possible too, is there any way to disable default
databinding of GridView/SqlDataSource combo to save extra trip to the
datatbase, given that I always have to rebind the gridview anyway?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top