i am getting id from the url and pass it to sqldatasource – selectcommand i get the following error :
Conversion failed when converting the varchar value '<%=MyIdVal%>'
to data type int.
code behind :
Public Partial Class Edit
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
MyIdVal = Request.QueryString("id")
End Sub
Private _myIdVal As Integer
Public Property MyIdVal() As Integer
Get
Return _myIdVal
End Get
Set(ByVal value As Integer)
_myIdVal = value
End Set
End Property
End Class
client :
< head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%=MyIdVal%>
</div>
<asp:GridView ID="GridView1" runat="server" DataSourceID="myIdDataSource">
</asp:GridView>
<asp:SqlDataSource runat="server" ID="myIdDataSource"
ConnectionString="<%$ ConnectionStrings:myCipConnection %>"
ProviderName="<%$ ConnectionStrings:myCipConnection.ProviderName %>"
SelectCommand="SELECT * FROM books WHERE id = '<%=MyIdVal%>'" >
</asp:SqlDataSource>
</form>
</body>
</html>
the code runs fine when i hardcode it
any idea how to fix this error thanks
You cannot use
<%= %>syntax in the select command. You have to use a parameter in the query, and add a parameter to the SelectParameters collection of the datasource like: