Sunday, February 19, 2012

GridView Help

Hi,

I use WVD and SQL Express 2005.

I have a table "SignIn" that one of fields inserted automatically by getdate()

And I have GridView that I use to display this table because I would like take advantage of GridView sorting and paging methods that are embedded in.

Currently I display all records at once.

My problem is how to make the GridView show today's records only.

I tried this code below, but I get only this message "There are no data records to display."

<asp:SqlDataSourceID="SqlDataSource1"runat="server"

ConnectionString="<%$ ConnectionStrings:RC1%>"

ProviderName="<%$ ConnectionStrings:RC1.ProviderName%>"

SelectCommand="SELECT [student_ID], [SignIn], [SignOut], [Location] FROM [Stud_data] WHERE (CONVERT(VARCHAR(10),[SignIn],101) = @.SignIn)">

<SelectParameters>

<asp:QueryStringParameterName="SignIn"QueryStringField="Format(Now, "M/dd/yyyy")" Type="DateTime"/>

</SelectParameters>

</asp:SqlDataSource>

Help Please!

I don't think you need use QueryStringParameter here. You can add the selectparameter from page load to assign today's date to it:

Like:

protectedvoid Page_Load(object sender,EventArgs e)

{

SqlDataSource1.SelectParameters.Add(

"SignIn",DateTime.Today.ToString("MM/dd/yyyy"));

}

}

The following is for a QueryStringParameter, but you don't need it.

SelectCommand="SELECT [student_ID], [SignIn], [SignOut], [Location] FROM [Stud_data] WHERE (CONVERT(VARCHAR(10),[SignIn],101) = @.SignIn)">

<SelectParameters>

<%

-- <asp:QueryStringParameter DefaultValue="01/01/2007" Name="SignIn" QueryStringField="mydate" Type="DateTime" />

--

%><asp:QueryStringParameterDefaultValue="1/1/2007"Name="SignIn"QueryStringField="mydate"Type="DateTime"/>

</SelectParameters>

Your URL will look something like this:

http://localhost:2013/WebSite1/queryDate.aspx?mydate=1/2/2006

|||

Thank you Limno,

SelectCommand="SELECT [student_ID], [SignIn], [SignOut], [Location] FROM [Stud_data] WHERE (CONVERT(VARCHAR(10),[SignIn],101) = @.SignIn)">

This SelectCommand solved my problem.Cool

I tried the

ProtectedSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)

SqlDataSource1.SelectParameters.Add("SignIn", DateTime.Today.ToString("MM/dd/yyyy"))

EndSub

And I inserted SelectParameters like this:

<asp:SqlDataSourceID="SqlDataSource1" . . .

.

.

.

<SelectParameters>

<asp:QueryStringParameterDefaultValue="1/1/2007"Name="SignIn"QueryStringField="mydate"Type="DateTime"/>

</SelectParameters>

</asp:SqlDataSource>

I keep getting the following error:

The variable name '@.SignIn' has already been declared. Variable names must be unique within a query batch or stored procedure

I wonder what I did wrong?

Thank you.

|||

alexmu06:

Thank you Limno,

SelectCommand="SELECT [student_ID], [SignIn], [SignOut], [Location] FROM [Stud_data] WHERE (CONVERT(VARCHAR(10),[SignIn],101) = @.SignIn)">

This SelectCommand solved my problem.Cool

I tried the

ProtectedSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)

SqlDataSource1.SelectParameters.Add("SignIn", DateTime.Today.ToString("MM/dd/yyyy"))

EndSub

And I inserted SelectParameters like this:

<asp:SqlDataSourceID="SqlDataSource1" . . .

.

.

.

<SelectParameters>

<asp:QueryStringParameterDefaultValue="1/1/2007"Name="SignIn"QueryStringField="mydate"Type="DateTime"/>

</SelectParameters>

</asp:SqlDataSource>

I keep getting the following error:

The variable name '@.SignIn' has already been declared. Variable names must be unique within a query batch or stored procedure

I wonder what I did wrong?

Thank you.

When you add the selectparameter from your code, you cannot use the declaratively again. You can use only one of them. I would prefer teh code one in your case and I hope this answers your question.

|||

Sorry but i don't get it.

if its possible could you expand a bit.

Thanks

|||

Hello:

SelectCommand="SELECT [student_ID], [SignIn], [SignOut], [Location] FROM [Stud_data] WHERE (CONVERT(VARCHAR(10),[SignIn],101) = @.SignIn)">

and the following is all you need.

ProtectedSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)

SqlDataSource1.SelectParameters.Add("SignIn", DateTime.Today.ToString("MM/dd/yyyy"))

EndSub

Please do not add that QueryStringParameter in your SelectParameters for this "SignIn"(you've already had it from the above code).

|||

Thank you,

I'll try that

Alex

No comments:

Post a Comment