This below Codings is to retrieve a data from ASP Datagrid. Those who are beginers to ASP.net this code will be very much useful to know.Please revert me if you facing any problem with this code.Let me try to help you :-)Please don't hesitate to go through all the codes..Hope just a copy paste of these codes in corresponding places will be works by changing your own connection string..Thanks :-)
ASPX Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function DO(ID) {
document.getElementById("hdnID").value = ID;
document.getElementById("form1").submit();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="100%">
<tr>
<td colspan="2" align="center">
<asp:DataGrid ID="grdview" runat="server" AutoGenerateColumns="false" OnItemDataBound="ItemBound">
<Columns>
<asp:BoundColumn DataField="ID" HeaderText="ID" Visible="false"></asp:BoundColumn>
<asp:BoundColumn DataField="NAME" HeaderText="NAME"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<input type="hidden" id="hdnID" runat="server" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
C# Code:
public partial class Ganesh : System.Web.UI.Page
{
SqlConnection con;
protected void Page_Load(object sender, EventArgs e)
{
try
{
con = new SqlConnection(@"data source=GANESH-VAIO;initial catalog=GANESH;user id=sa;password=cool");
if (!IsPostBack)
{
FillGrid();
}
else
{
if (!string.IsNullOrEmpty(hdnID.Value))
{
GetValue();
ViewState["ID"] = hdnID.Value;
hdnID.Value = "";
}
}
}
catch (Exception ex)
{
Response.Redirect(ex.Message);
}
}
public void GetValue()
{
DataSet ds;
try
{
ds = new DataSet();
con.Open();
SqlDataAdapter dr = new SqlDataAdapter("select id,name from study where id='" + hdnID.Value.Trim() + "'", con);
dr.Fill(ds);
if(ds!=null && ds.Tables.Count>0&&ds.Tables[0].Rows.Count>0)
{
txtName.Text = ds.Tables[0].Rows[0]["NAME"].ToString();
}
}
catch (Exception ex)
{
throw ex;
}
}
protected void ItemBound(object sender, DataGridItemEventArgs e)
{
TableRow dr;
try
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
dr = (TableRow)e.Item.Cells[0].Parent;
dr.Attributes.Add("OnClick", "DO('" + e.Item.Cells[0].Text + "');");
}
}
catch (Exception ex)
{
Response.Redirect(ex.Message);
}
}
protected void FillGrid()
{
DataSet ds;
try
{
ds = new DataSet();
con.Open();
//SqlDataAdapter dr = new SqlDataAdapter("select id,name from study", con);
//dr.Fill(ds);
//grdview.DataSource = ds;
SqlCommand cmd = new SqlCommand("select id,name from study", con);
SqlDataReader read = cmd.ExecuteReader();
grdview.DataSource = read;
grdview.DataBind();
}
catch (Exception ex)
{
throw ex;
}
}
}
DATABASE CODE :
CREATE TABLE STUDY(ID CHAR(16),NAME VARCHAR(100))
INSERT INTO STUDY (ID,NAME) VALUES (REPLACE((RIGHT(NEWID(),17)),'-',''),'GANESH')
INSERT INTO STUDY (ID,NAME) VALUES (REPLACE((RIGHT(NEWID(),17)),'-',''),'RAMESH')
INSERT INTO STUDY (ID,NAME) VALUES (REPLACE((RIGHT(NEWID(),17)),'-',''),'SURESH')
SELECT * FROM STUDY
Thanks & Regards,
Ganesh kumar K