Tuesday, March 27, 2012

Binding a SqlDataSource, to a GridView at Runtime

Hello

I'm experiencing some problems, binding a SqlDataSource to a GridView.

The following code creates the SqlDataSource:
string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string strProvider = ConfigurationManager.ConnectionStrings["ConnectionString"].ProviderName;

SqlDataSource ds = new SqlDataSource(strProvider, strConn);
ds.SelectCommand = "SELECT * FROM rammekategori";

Then i bind the SqlDataSource to a GridView:

GridView1.DataSource = ds;
GridView1.DataBind();

ErrorMessage:

Format of the initialization string does not conform to specification starting at index 0.

Exception Details: System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.

Line 24: GridView1.DataBind();

Am i totally off target here? Can it be something about, that you have to set the datasource of the gridview, before the Page_Load event?

Thanks - MartinHN

You are probably not going to be happy with the results if you do manage to fix your immediate error. May I suggest you actually put the SqlDataSource in the .aspx page? You can then bind it in code, but don't change the select statement in code, as that too will cause some issues.|||

oh. The code was copied from VS2005, i forgot one important detail.

I add the SqlDataSource to the page, by using Page.Controls.Add(ds);

Still, i get the error, even if i set the DataSource property of the gridview like this:

GridView1.DataSource = (SqlDataSource)Page.FindControl("ds");

--

MartinHN

|||

What if you try this:

SqlDataSource m_SqlDataSource = Page.FindControl("ds") as SqlDataSource;

if (m_SqlDataSource != null)
{
GridView1.DataSourceID = m_SqlDataSource.ID;
GridView1.DataBind();
}

HTH,
Ryan

|||

>>>>What if you try this:

SqlDataSource m_SqlDataSource = Page.FindControl("ds") as SqlDataSource;

if (m_SqlDataSource != null)
{
GridView1.DataSourceID = m_SqlDataSource.ID;
GridView1.DataBind();
}

Still no success... It is still the samme error message:Format of the initialization string does not conform to specification starting at index 0.

I was wondering, if i need to set a couple of more properties on my SqlDataSource.

One thing i just tried, was createing a new SqlDataSource directly on the aspx page, from the Designer in VS2005. The i set the datasource, to the FindControl method, with the name as the parameter. That worked. But i'm interessted in getting a SqlDataSource object, from lower tier, than the page it self. So that i have a complete SqlDataSource with select, update, delete and insert commands, to use along with the gridview...


|||

Ah. I totally missed the actual error. Please post your connection string, leaving out any login information, but keep the formatting as-is.

|||

ok...

The connectionstring i am using are as follows:

<connectionStrings>
<add name="ConnectionString" connectionString="DRIVER={MySQL ODBC 3.51 Driver};SERVER=myserverip;DATABASE=mydb;UID=myuid;PASSWORD=mypwd;" providerName="System.Data.Odbc" />
</connectionStrings>

Should there be any troubles using a mySQL DB?

|||

I think I see your problem. The overloads for the SqlDataSource on MSDN are as follows:

SqlDataSource()
SqlDataSource(string connectionString, string selectCommand)
SqlDataSource(string providerName, string connectionString, string selectCommand)

So, you should be doing something like this:

string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string strProvider = ConfigurationManager.ConnectionStrings["ConnectionString"].ProviderName;
string selectCommand = "SELECT * FROM rammekategori";

SqlDataSource ds = new SqlDataSource(strProvider, strConn, selectCommand);

HTH,
Ryan

|||

Ryan - I really cannot find a way to show my appreciation... That was very nice to have sorted out, thanks for your help.

What about insert, delete and update commands, can i set them after I've instantiated the SqlDataSource object?

|||

martinhn wrote:

What about insert, delete and update commands, can i set them after I've instantiated the SqlDataSource object?

Yes.

SqlDataSource.DeleteCommand
SqlDataSource.InsertCommand
SqlDataSource.UpdateCommand

HTH,
Ryan

sql

No comments:

Post a Comment