Saturday, March 24, 2012

Tag not well formed?

I keep getting a "Parser Error Message:The server tag is not well formed" message for this. Can anyone help me understand why?

<asp:SqlDataSource ID="ctrlDataSource" runat="server"
ProviderName="System.Data.OleDb"
ConnectionString = "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" & Server.MapPath("Database\TestDB.mdb")
Set DbConnection = Server.CreateObject("ADODB.Connection")
DbConnection.Open ConnectionString
SelectCommand="SELECT * FROM [Testtable] WHERE ID=@dotnet.itags.org.id" >

Is it the &? or Server.MapPath(@."Database\TestDB.mdb") ?


I don't think it is the &. I've seen it in sample code that uses Server.MapPath as part of the connectionstring. However, if not for the &, how would I make the Server.MapPath part of ConnectionString?

g2000:

or Server.MapPath(@."Database\TestDB.mdb") ?


I'm not sure what you mean...that there should be @. ?


doesn't it need the closing /

... /> instead of ...>


Actually, I close the tag later on, after SelectParameters. More completely, it looks like this:

<asp:SqlDataSource ID="ctrlDataSource" runat="server"
ProviderName="System.Data.OleDb"
ConnectionString = "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" & Server.MapPath("Database\TestDB.mdb")
Set DbConnection = Server.CreateObject("ADODB.Connection")
DbConnection.Open ConnectionString
SelectCommand="SELECT * FROM [Testtable] WHERE ID=@.id"
<SelectParameters>
<asp:QueryStringParameter Name="id" QueryStringField="id" />
</SelectParameters
</asp:sqldatasource


have you tried hard-coding the conn string, as a test?


Yes. I used

<asp:SqlDataSource ID="ctrlDataSource" runat="server"
ProviderName="System.Data.OleDb"
ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=e:\ectserver\abcuser\DataBase\Testdb.mdb"
SelectCommand="SELECT * FROM [Testtable] WHERE ID=@.id" >

Now that is a bogus path, because I don't know the real one (this is not my server). However, the error message I get back is "is not a valid path," which is what I wouldexpect since it's bogus. It no longer complains"tag is not well formed." So why is the hard-coded tag well-formed while the other one isn't? I think there must be something about the syntax of my original <asp:SqlDataSource> ?


I don't think SqlDataSource has properties like

Set DbConnection = Server.CreateObject("ADODB.Connection")
DbConnection.Open ConnectionString

I am just beginning with ASP.NET but i think you would have to use <%= Server.MapPath("Database\TestDB.mdb") %>


I don't think you can concatenate a connection string within the SqlDataSource tag.

Try creating a connection string and assigning it to the data source programmatically in your code-behind page.



Ted C:

Try creating a connection string and assigning it to the data source programmatically in your code-behind page.

OK. How would I do this?


While lookking for the exact answer to your question, I found how to set up your connection string in your Web.config file. You'd have the following as part of the Web.config...

<connectionStrings>
<add name="SqlConnectionString"
connectionString="Data Source=localhost\band;Initial Catalog=Players;
Persist Security Info=True;User ID=band;Password=letmein"
providerName="System.Data.SqlClient"/>
</connectionStrings>

... and then your SqlDataSource tag would look something like...

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SqlConnectionString %>"
SelectCommand="SELECT [PlayerName], [PlayerManufacturerID] FROM [Player]">
</asp:SqlDataSource>

EDIT: Except I don't know if that solves your problem of dynamically building the connection string...

More searching...


OK, ConnectionString is a property of the SqlDataSource object, so you first create a string variable and use it to build your connection string dynamically...

string myConnString = "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" + Server.MapPath("Database\TestDB.mdb");

...then you assign that string to the SqlDataSource's ConnectionString property...

SqlDataSource1.ConnectionString = myConnString;

You might even be able to do it in one line in the code-behind page.

SqlDataSource1.ConnectionString = "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" + Server.MapPath("Database\TestDB.mdb");

That's in C#, of course. You'd need to translate it into VB, if that's what you're using.


Ted C:

OK, ConnectionString is a property of the SqlDataSource object, so you first create a string variable and use it to build your connection string dynamically...

string myConnString = "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" + Server.MapPath("Database\TestDB.mdb");

...then you assign that string to the SqlDataSource's ConnectionString property...

SqlDataSource1.ConnectionString = myConnString;

I tried that a little earlier, when you first suggested that maybe I could not concatenate within the tag. I tried

<%

string cstr = "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" + Server.MapPath("TestDB.mdb");

SqlDataSource.ConnectionString = cstr;

%>

I also tried single-line. But I kept getting back the error "CS0120:An object reference is required for the nonstatic field, method, orproperty 'System.Web.UI.WebControls.SqlDataSource.ConnectionString.get'" which is why I wound up asking how do I do it...I am not familiar with this error?


Although OLEDB can be used with SQLDATASOURCE, however why not use AccessDataSource which is built for this purpose. Actually it inherits from sqldatasource.

An example

<asp:AccessDataSource id="InvoiceAccessDataSource" DataFile="~/App_Data/Northwind.mdb" runat="server" SelectCommand="[Employee Sales By Country]" SelectCommandType="StoredProcedure"> <SelectParameters> <asp:Parameter Name="Beginning Date" Type="DateTime" defaultValue="1/1/1997" /> <asp:Parameter Name="Ending Date" Type="DateTime" defaultValue="1/31/1997" /> </SelectParameters></asp:AccessDataSource><asp:GridView id="InvoiceGridView" runat="server" AutoGenerateColumns="True" DataSourceid="InvoiceAccessDataSource" />
http://msdn2.microsoft.com/en-us/library/8e5545e1.aspx
Should you wish to continue using Sql datasource then here is how to do it.
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html > <head runat="server"> <title>ASP.NET Example</title></head><body> <form id="form1" runat="server"> <asp:SqlDataSource id="SqlDataSource1" runat="server" DataSourceMode="DataReader" ConnectionString="<%$ ConnectionStrings:MyNorthwind%>" SelectCommand="SELECT LastName FROM Employees"> </asp:SqlDataSource> <asp:ListBox id="ListBox1" runat="server" DataTextField="LastName" DataSourceID="SqlDataSource1"> </asp:ListBox> </form> </body></html>
http://msdn2.microsoft.com/en-us/library/dz12d98w(vs.80).aspx
 
Hope that helps, please ping with any questions

0 comments:

Post a Comment