Search content within the blog

Thursday, December 4, 2008

Gridview basic operations

Store the connection string details in web.config file
<connectionStrings>
<remove name="somename"></remove>
<add name="somename" connectionString="Data Source=yourserver;Initial Catalog=databsename; User Id=sa;Password=sa" providerName="System.Data.SqlClient">

</add>

</connectionStrings>

ASPX Page contents
<div>
<asp:Label runat="server" ID="lblMsg">
</asp:Label>
<asp:GridView runat="server" ID="grdDetails" AutoGenerateColumns="false" AutoGenerateEditButton="true"
AutoGenerateSelectButton="true" AutoGenerateDeleteButton="true" OnRowCancelingEdit="GridViewOnCancel"
OnRowUpdating="GridViewOnUpdate" OnRowEditing="GridViewOnEdit" AllowPaging="true" PagerSettings-Mode="NumericFirstLast" PageSize="4"
OnSelectedIndexChanged="GridSelectedIndecChanged" SelectedIndex="-1" OnRowDeleting="GridViewDelete" OnPageIndexChanging="GridViewPageIndexChanged">
<SelectedRowStyle BackColor="LightBlue" Font-Italic="true" />
<PagerSettings Position="TopAndBottom" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Name
</HeaderTemplate>
<ItemTemplate>
<asp:HiddenField runat="server" ID="hdnLoginId" Value='<%# Eval("LoginId") %>' />
<%# Eval("LoginName") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Details
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td style="width: 100%">
<table border="0" cellpadding="0" cellspacing="0" style="width: 99%">
<tr>
<td style="width: 25%">
Password: </td>
<td style="width: 75%">
<%#Eval("Password")%>
</td>
</tr>
<tr>
<td style="width: 25%">
LoginName: </td>
<td style="width: 75%">
<%#Eval("LoginName")%>
</td>
</tr>
<tr>
<td style="width: 25%">
Secret Answer: </td>
<td style="width: 75%">
<%#Eval("SecretAnswer")%>
</td>
</tr>
</table>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Edit
</HeaderTemplate>
<ItemTemplate>
<%#Eval("RegisteredDate").ToString() == string.Empty ? DateTime.Now.ToString() : Eval("RegisteredDate")%>
</ItemTemplate>
<EditItemTemplate>
<asp:HiddenField runat="server" ID="hdnRegisteredDate" Value='<%# Eval("RegisteredDate") %>' />
<asp:TextBox runat="server" ID="txtResponseDate" Text='<%#Eval("RegisteredDate")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>

ASPX.CS
protected void Page_Load(object sender, EventArgs e)
{
DisplayGrid();
}
private void DisplayGrid()
{
string connectionString = ReturnConnectionString("somename"); //somename is the connection name given in web.config file
if (connectionString != string.Empty)
{
DataSet objDataSet = new DataSet();
SqlConnection objConnection = new SqlConnection(connectionString);
SqlCommand objCommand = new SqlCommand();
objCommand.CommandType = CommandType.StoredProcedure;
objCommand.CommandText = "dbo.[Loobi_Login_SelectAll]";
objCommand.Connection = objConnection;
// SqlParameter objParameter = new SqlParameter("@LoginId", SqlDbType.Int,4,ParameterDirection.Input,false,10,0,"",DataRowVersion.Proposed,7);
// objCommand.Parameters.Add(objParameter);
SqlDataAdapter objAdapTer = new SqlDataAdapter(objCommand);
objAdapTer.Fill(objDataSet);
if (objDataSet.Tables.Count > 0 && objDataSet.Tables[0].Rows.Count > 0)
{
grdDetails.DataSource = objDataSet.Tables[0].DefaultView;
grdDetails.DataBind();
}
else
{
lblMsg.Text = "No Records Found";
lblMsg.ForeColor = System.Drawing.Color.Red;
}
}
}
protected void GridViewOnCancel(object Sender, GridViewCancelEditEventArgs e)
{
grdDetails.EditIndex = -1;
DisplayGrid();
}
protected void GridViewOnUpdate(object Sender, GridViewUpdateEventArgs e)
{
if (e.RowIndex >= 0)
{
if (grdDetails.Rows[e.RowIndex].FindControl("hdnLoginId") != null)
{
int loginid =int.Parse(((HiddenField)grdDetails.Rows[e.RowIndex].FindControl("hdnLoginId")).Value.Trim());
string NewDate = ((HiddenField)grdDetails.Rows[e.RowIndex].FindControl("hdnRegisteredDate")).Value.Trim();
grdDetails.EditIndex = -1;
}
DisplayGrid();
}

}
protected void GridViewOnEdit(object Sender, GridViewEditEventArgs e)
{
grdDetails.EditIndex = e.NewEditIndex;
DisplayGrid();
}
protected void GridViewOnDelete(object Sender, GridViewDeleteEventArgs e)
{
if (e.RowIndex > 0)
{
if (grdDetails.Rows[e.RowIndex].FindControl("hdnLoginId") != null)
{
//deleet code goes here
DisplayGrid();
}
}

}
protected void GridSelectedIndecChanged(object Sender, EventArgs e)
{
int index= grdDetails.SelectedIndex;
GridViewRow rw = grdDetails.Rows[index];
if (rw != null && rw.FindControl("hdnLoginId") != null)
{
int logind = int.Parse(((HiddenField)rw.FindControl("hdnLoginId")).Value.Trim());
Page.RegisterStartupScript("KeySelected", "<script language='javascript' type='text/javascript'>alert('" + logind.ToString() + "')</script>");

}
else
{
lblMsg.Text = "Row is null";
lblMsg.ForeColor = System.Drawing.Color.Red;
}

}
protected void GridViewDelete(object Sender, GridViewDeleteEventArgs e)
{
if (grdDetails.Rows[e.RowIndex].FindControl("hdnLoginId") != null)
{
string loginid = ((HiddenField)grdDetails.Rows[e.RowIndex].FindControl("hdnLoginId")).Value;
Page.RegisterStartupScript("KeySelected", "<script language='javascript' type='text/javascript'>alert('"+loginid+"')</script>");

}
}
protected void GridViewPageIndexChanged(object Sender, GridViewPageEventArgs e)
{
grdDetails.PageIndex = e.NewPageIndex;
DisplayGrid();
}

private string ReturnConnectionString(string connectionStringName)
{
System.Configuration.Configuration webConfig = null;
webConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
if (webConfig != null)
{
System.Configuration.ConnectionStringSettings connString = null;
if (webConfig.ConnectionStrings.ConnectionStrings.Count > 0)
{
connString = webConfig.ConnectionStrings.ConnectionStrings[connectionStringName];
if (connString != null && connString.ToString() != string.Empty)
{
return connString.ConnectionString;

}
else
{
lblMsg.Text = "The value of connection string in web.config file is null or empty";
lblMsg.ForeColor = System.Drawing.Color.Red;
return string.Empty;


}

}
else
{
lblMsg.Text = "No connection strings present in web.config file";
lblMsg.ForeColor = System.Drawing.Color.Red;
return string.Empty;

}
}
else
{
lblMsg.Text = "Configuration file missing";
lblMsg.ForeColor = System.Drawing.Color.Red;
return string.Empty;
}

}

No comments:

Post a Comment