How to select a record in the datagrid and start editing/updating the record using textboxes
<asp:DataGrid id=’DataGrid1′ OnItemCommand=’ItemCommand’ style=’Z-INDEX: 101; LEFT: 15px; POSITION: absolute; TOP: 23px’ runat=’server’ > <Columns> <asp:ButtonColumn Text=’Edit’ ButtonType=’PushButton’ CommandName=’Edit’> </asp:ButtonColumn> </Columns> </asp:DataGrid> <asp:TextBox id=’txtRegionID’ style=’Z-INDEX: 102; LEFT: 352px; POSITION: absolute; TOP: 24px’ runat=’server’></asp:TextBox> <asp:TextBox id=’txtRegionDescription’ style=’Z-INDEX: 103; LEFT: 352px; POSITION: absolute; TOP: 64px’ runat=’server’></asp:TextBox> <asp:Button id=’btnUpdate’ style=’Z-INDEX: 104; LEFT: 304px; POSITION: absolute; TOP: 128px’ runat=’server’ Text=’Update’></asp:Button> <asp:Label id=’lblMessage’ style=’Z-INDEX: 105; LEFT: 296px; POSITION: absolute; TOP: 176px’ runat=’server’></asp:Label> VB.NET Dim cn As SqlConnection Dim da As SqlDataAdapter Dim cmd As SqlCommand Dim strsql As String Dim ds As DataSet Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ’Put user code to initialize the page here cn = New SqlConnection(‘Server=localhost;uid=sa;pwd=;database=northwind;’) If Not Page.IsPostBack Then BindData() End If End Sub Sub BindData() DataGrid1.DataSource = GetData(‘Select * from Region’) DataGrid1.DataBind() End Sub Function GetData(ByVal strSql As String) As DataSet da = New SqlDataAdapter(strSql, cn) ds = New DataSet() da.Fill(ds) Return ds End Function Protected Sub ItemCommand(ByVal source As Object, _ ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) If e.CommandName = ‘Edit’ Then ’Fill the Textboxes with relevant data FillTheData(e.Item.Cells(1).Text, e.Item.Cells(2).Text) End If End Sub Sub FillTheData(ByVal RegionID As String, ByVal RegionDescription As String) txtRegionID.Text = RegionID txtRegionDescription.Text = RegionDescription End Sub Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click Try strsql = ‘Update Region set RegionDescription=@RegionDescription where RegionId=@RegionId’ cmd = New SqlCommand(strsql, cn) cmd.Parameters.Add(New SqlParameter(‘@RegionId’, SqlDbType.Int)) cmd.Parameters.Add(New SqlParameter(‘@RegionDescription’, SqlDbType.NVarChar, 40)) cmd.Parameters(‘@RegionId’).Value = Convert.ToInt32(txtRegionID.Text) cmd.Parameters(‘@RegionDescription’).Value = txtRegionDescription.Text cn.Open() cmd.ExecuteNonQuery() BindData() lblMessage.Text = ‘Updated Successfully’ Catch ex As Exception lblMessage.Text = ex.Message lblMessage.ForeColor = Color.Red Finally cn.Close() End Try End Sub C# SqlConnection cn; SqlDataAdapter da ; SqlCommand cmd ; string strsql ; DataSet ds ; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here cn = new SqlConnection(‘Server=localhost;uid=sa;pwd=;database=northwind;’); if(!Page.IsPostBack) { //Code to Bind the data to the Datagrid BindData(); } } void BindData() { DataGrid1.DataSource = GetData(‘Select * from Region’); DataGrid1.DataBind(); } DataSet GetData(string strSql) { da = new SqlDataAdapter(strSql, cn); ds = new DataSet(); da.Fill(ds); return ds; } protected void ItemCommand(Object source,System.Web.UI.WebControls.DataGridCommandEventArgs e) { if (e.CommandName == ‘Edit’) { //’Fill the Textboxes with relevant data FillTheData(e.Item.Cells[1].Text, e.Item.Cells[2].Text); lblMessage.Text=”; } } void FillTheData(string RegionID,string RegionDescription) { txtRegionID.Text = RegionID; txtRegionDescription.Text = RegionDescription; } private void btnUpdate_Click(object sender, System.EventArgs e) { try { strsql = ‘Update Region set RegionDescription=@RegionDescription where RegionId=@RegionId’; cmd = new SqlCommand(strsql, cn); cmd.Parameters.Add(new SqlParameter(‘@RegionId’, SqlDbType.Int)); cmd.Parameters.Add(new SqlParameter(‘@RegionDescription’, SqlDbType.NVarChar, 40)); cmd.Parameters[‘@RegionId’].Value = Convert.ToInt32(txtRegionID.Text); cmd.Parameters[‘@RegionDescription’].Value = txtRegionDescription.Text; cn.Open(); cmd.ExecuteNonQuery(); BindData(); lblMessage.Text = ‘Updated Successfully’; } catch (Exception ex) { lblMessage.Text = ex.Message; lblMessage.ForeColor = Color.Red; } finally { cn.Close(); } }
When I try to do an Update from my Datagrid, I keep getting the old/original values. Why?
This could happen if you are calling .DataBind everytime the Page is loaded To avoid this call Databind only for the first request and not for subsequent postbacks. VB.NET if not Page.IsPostBack then ’Your databinding code end if C# if (!Page.IsPostBack) { ’Your databinding code }
Why do I get the error message ‘Exception Details: System.Web.HttpException: Cache is not available ‘
Try using ‘HttpContext.Current.Cache’ instead of just ‘Cache’ in your code. The compiler probably wasn’t able to resolve the Cache type.
When using FormsAuthentication, how can I redirect a user to a different page other than the default page
When FormsAuthentication is used in ASP.NET, by default, it gets redirected to the default.aspx page. To redirect it to some other page other than default.aspx you have to use the code below. VB.NET FormsAuthentication.SetAuthCookie(txtUsername.Text,false) Response.Redirect(‘someotherpage.aspx’) C# FormsAuthentication.SetAuthCookie(txtUsername.Text,false); Response.Redirect(‘someotherpage.aspx’);
Why do I get error message ‘System.Web.HttpException: Request is not available in this context’ ‘ when using Cookies
Try using ‘System.Web.HttpContext.Current.Request.Cookies’ instead of ‘Request.Cookies’ in your code.