Showing posts with label control. Show all posts
Showing posts with label control. Show all posts

Friday, March 9, 2012

How to get Read only control value through SQl data Source

Hello,

I am using this sql data source control.

<asp:textboxrunat="server"id="txtFromDate" ReadOnly="true"></></asp:textbox>

<asp:textboxrunat="server"id="txtToDate" ReadOnly="true"></asp:textbox>

---------->

<asp:sqldatasourceid="dsClickInfo"runat="server"connectionstring="<%$ ConnectionStrings:activeConnectionString %>"

selectcommand="SProc_GetTransaction"selectcommandtype="StoredProcedure"><SelectParameters><asp:QueryStringParameterQueryStringField="Id"Name="MerchantID"Type="int32"/><asp:ControlParameterControlID="txtFromDate"Name="StartDate"PropertyName="Text"Type="String"DefaultValue="0"/><asp:ControlParameterControlID="txtToDate"Name="EndDate"PropertyName="Text"Type="String"DefaultValue="0"/></SelectParameters>

</asp:sqldatasource>

I abouve code txtFromDate and txtToDate are marked as readonly so sqldatasource is not able to get value from these controls.

How is it possible?

Please help me..

Hi Welcome to asp.net

I don't know the way you get value from these controls.

When I bind a textbox to field I'd like to set Enabled to false instead of the ReadOnly ,you can have a try,

<asp:TextBox ID="TextBox1" runat="server" Enabled=false Text='<%# Bind("contact_firstno") %>'></asp:TextBox>

|||

Thanks for reply..

But my problem is different..

I dont want to bind the text box. I am getting the text box value in sqldatasource control by the property

<SelectParameters>

<asp:QueryStringParameterQueryStringField="Id"Name="MerchantID"Type="int32"/>

<asp:ControlParameterControlID="txtFromDate"Name="StartDate"PropertyName="Text"

Type="String"DefaultValue="0"/>

<asp:ControlParameterControlID="txtToDate"Name="EndDate"PropertyName="Text"

Type="String"DefaultValue="0"/>

</SelectParameters>

and after that these values are passing to the DB (by the select command) for search a result according to the from date and to date.

Now if i am not making thetxtFromDate and txtToDatereadonly then getting proper result.

But if these are readonly then after click on the search button i am getting these controls values empty and dont get any changes in result according to search date.

Thanks

Rahul Panwar

|||

Sorry I misunderstood you.

Make sure Text in txtFromDate and txtToDate are only modified at ther server side (not by javascript...)

And debug your webapp to make sure the textbox value is right when you click search button .

And also make sure yourenableviewstateisnot set to false,like this:

<%@.Pagelanguage="c#"enableviewstate="false"Trace="true"%>

Hope it helps.

Wednesday, March 7, 2012

How to get number of records a query has returned using SqlDataSource control?

Hi,

I'm using SqlDataSource control.

Is there a way to know how many records a query has returned?

Try this one to get the total Rows Returned

This is a simple trick about how you can get the total of rows returned from your SqlDataSource control's SelectCommand query.

When you bind a data-source control to a GridView control and use paging etc, you can't use the GridView's Rows property to get the number of rows returned from your data-source control. The Rows property will only return the rows rendered by the GridView control. To get the total number of rows returned from the SelectCommand, you can hook up to the SqlDataSource's Selected event. The Selected event's SqlDataSourceStatusEventArgs event argument's AffectedRows property will return the number of totals rows the SelectCommand returnes. The following example is a simple page with a GridView, SqlDataSource control and a label control. The lable control will be used to display the total number of rows the SelectComamand of the SqlDataSource control returns.

<%@. Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="Server">

protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)

{

totalRows.Text = e.AffectedRows.ToString();

}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID"

DataSourceID="SqlDataSource1" AllowPaging="True">

<Columns>

<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />

<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />

<asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />

</Columns>

</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=(local);Initial Catalog=Northwind;Integrated Security=True"

ProviderName="System.Data.SqlClient" SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName] FROM [Customers]" OnSelected="SqlDataSource1_Selected">

</asp:SqlDataSource>

<asp:Label ID="Label1" runat="server" Text="Number of total rows:"></asp:Label>

<asp:Label ID="totalRows" runat="server" Text="Label"></asp:Label>

</div>

</form>

</body>

</html>

Hope this will help you

Satya