If you got this error message when sorting a Gridview:
The GridView 'GridView1' fired event sorting which wasn't handled.
Then do the following add/changes in your code behind.
Aspx.cs Page:
public SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;
return (SortDirection)ViewState["sortDirection"];
}
set { ViewState["sortDirection"] = value; }
}
protected void Gridview1_Sorting(object sender, GridViewSortEventArgs e)
{
string strSortDir = null;switch (GridViewSortDirection)
{
case SortDirection.Ascending:GridViewSortDirection = SortDirection.Descending;
strSortDir = "DESC";
break;
case SortDirection.Descending:GridViewSortDirection = SortDirection.Ascending;
strSortDir = "ASC";
break;
}
SqlDataSource1.SelectCommand = "SELECT ...... FROM tableName order by " + e.SortExpression + " " + strSortDir;
}
More: http://forums.asp.net/t/1286994.aspx
Monday, December 15, 2008
Saturday, November 15, 2008
Parameterized SQL Query in sqldatasource and bind with Gridview
To avoid SQL injections you have to write stored procedure or the parametrized query. If you have a sqldatasource, want to pass the paramterized query to that datasource and finnally display with Gridview...Just customize your code like follow:
string sql = "SELECT Guid as Guid,[ID], [Family Name] AS Family_Name, [Given Name] AS Given_Name, [Region], [MacroRegion], [Country], [Years], [Date] FROM [Author] where [Given Name] LIKE '%' + @Given_Name + '%'";
SqlDataSource1.SelectCommand = sql;
SqlDataSource1.SelectParameters.Clear();
SqlDataSource1.SelectParameters.Add("Given_Name", TypeCode.String, searchTextBox.Text.Trim());
GridView1.DataSource = SqlDataSource1;
GridView1.DataSourceID = string.Empty;
GridView1.DataBind();
More:http://forums.asp.net/t/1309562.aspx
string sql = "SELECT Guid as Guid,[ID], [Family Name] AS Family_Name, [Given Name] AS Given_Name, [Region], [MacroRegion], [Country], [Years], [Date] FROM [Author] where [Given Name] LIKE '%' + @Given_Name + '%'";
SqlDataSource1.SelectCommand = sql;
SqlDataSource1.SelectParameters.Clear();
SqlDataSource1.SelectParameters.Add("Given_Name", TypeCode.String, searchTextBox.Text.Trim());
GridView1.DataSource = SqlDataSource1;
GridView1.DataSourceID = string.Empty;
GridView1.DataBind();
More:http://forums.asp.net/t/1309562.aspx
Tuesday, October 28, 2008
Creating A-Z lists
If you want to creat A-Z lists in a Repeater control and implement so that people can click that charater and get value from database which start from that clicked charater.
ASPX Page:

Aspx.cs Page:
protected void Repeater1_RowCreated(object sender, RepeaterItemEventArgs e)
{
//Create A-Z listArrayList ab = new ArrayList();for (int i = 0; i < datasource =" ab;Repeater2.DataSourceID">
protected void Repeater1_RowCommand(object sender, RepeaterCommandEventArgs e)
{
if (e.CommandName.Equals("AlphaPaging"))
{
//convert ASCII value to char/string.i.e. A=>65
int index;
index = Convert.ToInt32(e.CommandArgument);
String abc = Char.ConvertFromUtf32(index);
String sql = "select Title,Scope,CollectionAbstract,CollectionIdentifier" +
" from tblCollections_Collections" +" where RepositoryID in( " + repository + " ) and Title LIKE '" + abc + "%'";
Response.Write(sql);
SqlConnection conn = new SqlConnection(DataAccess.GetConnectionString());
conn.Open();
SqlDataAdapter ad = new SqlDataAdapter(sql, conn);
ad.SelectCommand = new SqlCommand(sql, conn);
DataSet ds = new DataSet();
ad.Fill(ds);
Repeater1.DataSource = ds;
Repeater1.DataSourceID = string.Empty;
Repeater1.DataBind();
conn.Close();
}
}
}
Result:
ASPX Page:

protected void Repeater1_RowCreated(object sender, RepeaterItemEventArgs e)
{
//Create A-Z listArrayList ab = new ArrayList();for (int i = 0; i < datasource =" ab;Repeater2.DataSourceID">
protected void Repeater1_RowCommand(object sender, RepeaterCommandEventArgs e)
{
if (e.CommandName.Equals("AlphaPaging"))
{
//convert ASCII value to char/string.i.e. A=>65
int index;
index = Convert.ToInt32(e.CommandArgument);
String abc = Char.ConvertFromUtf32(index);
String sql = "select Title,Scope,CollectionAbstract,CollectionIdentifier" +
" from tblCollections_Collections" +" where RepositoryID in( " + repository + " ) and Title LIKE '" + abc + "%'";
Response.Write(sql);
SqlConnection conn = new SqlConnection(DataAccess.GetConnectionString());
conn.Open();
SqlDataAdapter ad = new SqlDataAdapter(sql, conn);
ad.SelectCommand = new SqlCommand(sql, conn);
DataSet ds = new DataSet();
ad.Fill(ds);
Repeater1.DataSource = ds;
Repeater1.DataSourceID = string.Empty;
Repeater1.DataBind();
conn.Close();
}
}
}
Result:
Wednesday, October 15, 2008
Both DataSource and DataSourceID are defined on ‘GridView1′. Remove one definition.
This is the message that you get most of the times when you try to bind your gridview or any other bindable controls to multiple Datasources.When you bind your Gridview control to an ObjectDataSource or SqlDataSource on the design surface and then you try to bind the same Gridview in the code behind using the manual coding.
The Gridview has both Datasource and DatasourceID properties.Datasource referes to the ID of another component such as SqlDataSource/XMLDataSource/AccessDataSource, whichever applies, or ObjectDataSource to act as the Datasource.
When you have the following code:
SqlDataSource1.DataBind();
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
You'll get this error:
Both DataSource and DataSourceID are defined on ‘GridView1′. Remove one definition.
Both DataSource and DataSourceID are defined on ‘GridView1′. Remove one definition.
To make working,
Just add a line of code before DataBind():
GridView1.DataSourceID = string.Empty;
GridView1.DataSourceID = string.Empty;
Now, you are fine, no more error......
Happy Programming!!
Subscribe to:
Posts (Atom)