Showing posts with label sqldatasource. Show all posts
Showing posts with label sqldatasource. Show all posts

Monday, March 12, 2012

How to Set Start Index in a Data Control which is attahced to a Data Component

Hello All,

I have a SQLDataSource "sdsX" which has four records. There is a DataList "dlX" which is binded to this SQLDataSource "sdsX". Now I want that my datalist "dlX" take third record of "sdsX" as its first element. Is there any property which can be used.

Thanks

Hi ShailAtlas,

Base on my understanding, you want to move the third row to first row in DataList. For example:

Here is a DataList rendered likes below:

----------

1 | name1 |

----------

2 | name2 |

----------

3 | name3 |

----------

4 | name4 |

----------

You want to render it likes below without any change of datasource.

----------

3 | name3 |

----------

1 | name1 |

----------

2 | name2 |

----------

4 | name4 |

----------

If I have misunderstood your concern, please feel free to let me know.

DataList doesn't have property for this special request. If you want to implement it, you should add your own code in DataList_PreRender event handler. Here is the sample code:

int i = 0;

protectedvoid Page_Load(object sender,EventArgs e)

{

DataList1.DataBind();

}

protectedvoid Button1_Click(object sender,EventArgs e)

{

i =int.Parse(TextBox1.Text);

}

protectedvoid DataList1_PreRender(object sender,EventArgs e)

{

if (i != 0)

{

int j = i - 1;

string backupid = ((Label)DataList1.Items[j].Controls[1]).Text;

string backupname = ((Label)DataList1.Items[j].Controls[3]).Text;

for (; j > 0; j--)

{

((Label)DataList1.Items[j].Controls[1]).Text = ((Label)DataList1.Items[j-1].Controls[1]).Text;

((Label)DataList1.Items[j].Controls[3]).Text = ((Label)DataList1.Items[j-1].Controls[3]).Text;

}

((Label)DataList1.Items[0].Controls[1]).Text = backupid;

((Label)DataList1.Items[0].Controls[3]).Text = backupname;

}

}

When you input 3 in TextBox1 and then press Button1. DataList begin to render with the special order.

|||

Hello Ben,

Lets understand like this.I have a SQLDataSource which has 4 records. Now I have 2 datalist say DataListX and DataListY.

I want that DataListX should show record number 1 & 2, and DataListY should show records 3 & 4

SQLDataSource has records like this

----------

1 | name1 |

----------

2 | name2 |

----------

3 | name3 |

----------

4 | name4 |

----------

Now DataListX will show like this

----------

1 | name1 |

----------

2 | name2 |

And DataListY should be like this

----------

3 | name3 |

----------

4 | name4 |

Intention is that I do not want to use 2 SQLDataSource for same type of records

Thanks for your Reply

Shail

How to set SQLCommand timeout for SqlDataSource for ASP.NET 2.0?

With VS2005, there is a new component SqlDataSource,

<asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>

Then you can assign SP and bind datasource to a get data for this component in .NET code:

SqlDataSource1.SelectCommand = "spName"
SqlDataSource1.SelectCommandType = SqlDataSourceCommandType.StoredProcedure
SqlDataSource1.ConnectionString = Comm.connString

There is no way to set sqlcommand timeout for this stored procedure like SqlClient.SqlCommand. How can I do this?

Inside the SqlDataSource1_Selecting event, you get access to the sqlcommand object via e.command. You can set the commandtimeout on it there, like:

e.Command.CommandTimeout=300

|||

Thanks for your reply. But when is the event Selecting fired? Is it fired automatically when binding to data source?

|||

Hi KentZhou,

The sqldatasource.selecting event happends right before your perform a data retrieval opteration(where you execute your select command).You can refer to the msdn explaination:

msdn:

Occurs before a data retrieval operation.

Handle theSelecting event to perform additional initialization operations that are specific to your application, to validate the values of parameters, or to change the parameter values before theSqlDataSource control performs the select operation. The select arguments are available from theSqlDataSourceSelectingEventArgs object that is associated with the event.

Hope my suggestion helps

|||

The short answer is "Yes".

It fires any time that you do a databind.

The long answer as described above, it actually happens any time that the SqlDatasource is about to go and execute your select command. This is normally done through a databinding operation (Either implied or programmatically triggered), however it can also be triggered if you call the select method on the SqlDatasource directly.