Showing posts with label gridview. Show all posts
Showing posts with label gridview. Show all posts

Friday, March 30, 2012

parameter problem

Hi, i want to bind dropdowenlist ti gridview so i fill grid with aprameter takes from dropdownlist

i use this code to defien the parameter

SqlParameter pram1 =newSqlParameter("@.Group_ID",SqlDbType.Int, 4,"Group_ID");

pram1.Direction =ParameterDirection.Input;

pram1.Value =Convert.ToInt32(DropDownList5.SelectedItem.Value);

cmd.Parameters.Add(pram1);

what is th problem in value????

i try with many thing but the same result

plz help,,,

Hello my friend,

Use the following line: -

cmd.Parameters.Add("@.Group_ID", SqlDbType.Int, 4).Value = DropDownList5.SelectedItem.Value;

I would be sure to test that the dropdown list has a selected value or your code will crash: -

if (DropDownList5.SelectedIndex >= 0)

Kind regards

Scotty

|||

I agree. Something like:

cmd.Parameters.Add(
"@.Group_ID"
, SqlDbType.Int
).Value = (DropDownList5.SelectedIndex >= 0) ? DropDownList5.SelectedItem.Value : DBNull.Value;

Of course, your sql statement/sproc should consider @.Group_ID could be NULL as well.

|||

Try to use

DropDownList5.SelectedValue instead ofDropDownList5.SelectedItem.Value

Satya

|||

In order to know whether an items was selected (checking SelectedIndex >= 0) or not (setting the parameter to DBNull.Value) then you would be doing extra checks on SelectedIndex (check Reflector for the exact code). A minor point, but something to consider.

Tuesday, March 20, 2012

Paging with Gridview and ObjectDataSource

I have a problem with efficiently paging with gridview and objectdatasoruce. I have GetPosts1(startRowIndex, maximumRow, topic_id) and GetPostsCount(topic_id). I tested each procedure and each are working correctly. The problem is with the controls. Here is the code for the controls.

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames
DataSourceID="ObjectDataSource2">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="RowNumber" SortExpression="RowNumber" />
<asp:BoundField DataField="post_id" HeaderText="post_id" SortExpression="post_id" />
<asp:BoundField DataField="post_subject" HeaderText="post_subject" SortExpression="post_subject" />
<asp:BoundField DataField="post_text" HeaderText="post_text" SortExpression="post_text" />
<asp:BoundField DataField="post_time" HeaderText="post_time" SortExpression="post_time" />
<asp:BoundField DataField="topic_id" HeaderText="topic_id" SortExpression="topic_id" />
<asp:BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName" />
<asp:BoundField DataField="UserID" HeaderText="UserID" SortExpression="UserID" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" OldValuesParameterFormatString="original_{0}"
EnablePaging="True" SelectMethod="GetPosts1" SelectCountMethod="GetPostsCount" TypeName="PostsTableAdapters.discussions_GetPostsTableAdapter">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="48" Name="topic_id" QueryStringField="t" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>

When I run the page, I get "A first chance exception of type 'System.InvalidOperationException' occurred in System.Data.dll" and then "The thread '<No Name>' (0xbe0) has exited with code 0 (0x0)."

Could the problem be with null or empty values in the returned data?

Here is an example which work:

<asp:ObjectDataSourceID="odsObjectDataSource"runat="server"SelectMethod="GetResults"TypeName="ResultsList">

<SelectParameters>

<asp:ParameterDefaultValue="0"Name="StartRow"Type="Int32"/>

<asp:SessionParameterName="xslSearch"SessionField="xsltPathShowRezults"Type="String"/>

</SelectParameters>

</asp:ObjectDataSource>

Monday, March 12, 2012

Paging with Gridview and ObjectDataSource

I'm trying to effecinty page through many rows of data with the gridview and objectdatasource. I'm having trouble. I'm using a table adapter with predefined counting and select methods. I have tested all the methods and they all work properly. But when I configure the object datasource to use the table adapter, and set the gridviews datasrouce, the page doesn't load and I wind up getting "time out". Any help?

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" DataSourceID="objTopics">
<Columns>
<asp:BoundField DataField="topic_title" />
</Columns>
<EmptyDataTemplate>
<p>NOTHING HERE</p>
</EmptyDataTemplate>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" EnablePaging="True" OldValuesParameterFormatString="original_{0}"
SelectMethod="GetTopics" SelectCountMethod="GetTopicsRowCount" TypeName="TopicsTableAdapters.discussions_GetTopicsSubSetTableAdapter">
<SelectParameters>
<asp:Parameter DefaultValue="1" Name="startRowIndex" Type="Int32" />
<asp:Parameter DefaultValue="10" Name="maximumRows" Type="Int32" />
<asp:Parameter DefaultValue="1" Name="board_id" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>

<asp:GridViewID="gvResult"

runat="server"AutoGenerateColumns="False"CssClass="dataGrid"

OnSelectedIndexChanged="GridView1_SelectedIndexChanged"OnRowDataBound="gvResult_RowDataBound"AllowPaging="True"AllowSorting="True"DataSourceID="odsObjectDataSource"OnPageIndexChanged="gvResult_PageIndexChanged"PageSize="5">

<Columns>

<asp:ButtonFieldCommandName="Select"HeaderText="Select"Text="<img src='../IMAGES/select.jpg'/>"/>

<asp:BoundFieldDataField="SocieteId"HeaderText="Identifiant"ReadOnly="True"/>

<asp:BoundFieldDataField="SocieteLocalite"HeaderText="Localité"ReadOnly="True"/>

</Columns>

<AlternatingRowStyleCssClass="alt"/>

<PagerSettingsMode="NextPreviousFirstLast"/>

</asp:GridView>

<asp:LabelID="lblError"runat="server"Text=""></asp:Label></div>

<asp:ObjectDataSourceID="odsObjectDataSource"runat="server"SelectMethod="GetResults"TypeName="ResultsList">

<SelectParameters>

<asp:ParameterDefaultValue="0"Name="StartRow"Type="Int32"/>

<asp:SessionParameterName="xslSearch"SessionField="xsltPathShowRezults"Type="String"/>

</SelectParameters>

</asp:ObjectDataSource>

For the object datasource you have to writye a class wich will get data, like

privateDataView GetData(int StartRow,string xslSearch)

{

//your code here

return PagedResultsTable.DefaultView;

}

and this method is set in the design for the object datasource.