Hi All,
I need to insert the gridview row values into DB.
this is my Gridview******************
<asp:GridView ID="GrdOptions" runat="server" AutoGenerateColumns="False"
Width="212px" ShowHeader="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txttask" runat="server" CssClass="Text" MaxLength="500"
Width="350px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txthrs" runat="server" CssClass="Text" MaxLength="500"
Width="80px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
***************************************************
i need to check the each row that how many rows have tha values based on this only i need to insert only that row has value.
Thanks in advance
Loading
Satyapriya NayakPosted Jan 3, 2012, 4:12 AM
Try this...
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected void btnUpdate_Click(object sender, EventArgs e)
{
StringBuilder strSql = new StringBuilder(string.Empty);
SqlConnection con = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkUpdate = (CheckBox)
GridView1.Rows[i].Cells[0].FindControl("chkSelect");
if (chkUpdate != null)
{
if (chkUpdate.Checked)
{
string strID = GridView1.Rows[i].Cells[1].Text;
string strName = ((TextBox)
GridView1.Rows[i].FindControl("txtName")).Text;
string strLocation = ((TextBox)
GridView1.Rows[i].FindControl("txtLocation")).Text;
string strUpdate =
"Update Details set Name = '" + strName + "',Location = '" + strLocation + "' WHERE ID ='" + strID + "'";
strSql.Append(strUpdate);
}
}
}
try
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = strSql.ToString();
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
string errorMsg = "Error in Updation";
errorMsg += ex.Message;
throw new Exception(errorMsg);
}
finally
{
con.Close();
}
UncheckAll();
}
private void UncheckAll()
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chkUncheck = (CheckBox)
row.FindControl("chkSelect");
TextBox txtname = (TextBox)
row.FindControl("txtName");
TextBox txtlocation = (TextBox)
row.FindControl("txtLocation");
chkUncheck.Checked = false;
txtname.ReadOnly = true;
txtlocation.ReadOnly = true;
txtname.ForeColor = System.Drawing.Color.Blue;
txtlocation.ForeColor = System.Drawing.Color.Blue;
}
}
protected void chkSelect_CheckedChanged
(object sender, EventArgs e)
{
CheckBox chkTest = (CheckBox)sender;
GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;
TextBox txtname = (TextBox)grdRow.FindControl
("txtName");
TextBox txtlocation = (TextBox)grdRow.FindControl
("txtLocation");
if (chkTest.Checked)
{
txtname.ReadOnly = false;
txtlocation.ReadOnly = false;
txtname.ForeColor = System.Drawing.Color.Black;
txtlocation.ForeColor = System.Drawing.Color.Black;
}
else
{
txtname.ReadOnly = true;
txtlocation.ReadOnly = true;
txtname.ForeColor = System.Drawing.Color.Blue;
txtlocation.ForeColor = System.Drawing.Color.Blue;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
CheckState(true);
}
protected void Button2_Click(object sender, EventArgs e)
{
CheckState(false);
}
private void CheckState(bool p)
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chkcheck = (CheckBox)row.FindControl("chkSelect");
chkcheck.Checked = p;
}
}
}
Thanks
Krishna GaradPosted Jan 3, 2012, 2:19 AM
GridViewRow row = (GridViewRow)GrdOptions.Rows[e.RowIndex];
TextBox txttask = (TextBox)row.FindControl("txttask");
TextBox txthrs = (TextBox)row.FindControl("txthrs");
now check the text in the textbox
if(txthrs.Text=="")
//do nothing
else
//do somthing
like this for each row you can check it.