TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Kamala Veni
NA
65
21.5k
How to clear the error in update query
Jun 12 2016 8:08 AM
HI
i am have a grid view with edit,update,delete button edit and delete button is wrking,updated button is nt wrking....in my table i dnt have id columns,here showing negative error on [
string id = GridView1.DataKeys[e.RowIndex].Value.ToString();] this line...
hw to clear the error
hw to wrk?
my query:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
OnRowCancelingEdit="GridView1_RowCancelingEdit" CellPadding="4" ForeColor="#333333" OnRowDataBound="GridView1_RowDataBound"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" Height="199px" Width="516px">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LkB1" runat="server" CommandName="Edit">Edit</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LB2" runat="server" CommandName="Update">Update</asp:LinkButton>
<asp:LinkButton ID="LB3" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "S.NO" >
<ItemTemplate>
<asp:Label ID="lblRowNumber" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="HTSC">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("HTSCNO") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_Name" runat="server" Text='<%# Eval("HTSCNO") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ADDRESS1">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("ADDRESS1") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_Branch" runat="server" Text='<%# Eval("ADDRESS1") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ADDRESS2">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("ADDRESS2") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_City" runat="server" Text='<%# Eval("ADDRESS2") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
</asp:GridView>
public partial class As : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"data source=VAAYUSEVA_SVR;Initial Catalog=cppsch;User ID=sa;Password=sa");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
protected void BindGridView()
{
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("Select [USER NAME],HTSCNO,ADDRESS1,ADDRESS2 from TBL_CPPMASTER", con);
con.Open();
da.Fill(dt);
con.Close();
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGridView();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//find student id of edit row
string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
// string un = Session["Uname"].ToString();
TextBox name = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_Name");
TextBox branch = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_Branch");
TextBox city = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_City");
SqlCommand cmd = new SqlCommand("update TBL_CPPMASTER set HTSCNO='" + name.Text + "', ADDRESS1='"+branch.Text+"', ADDRESS2='"+city.Text+"' where [COMPANYNAME]= id", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
BindGridView();
}
// cancel row edit event
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGridView();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
(e.Row.FindControl("lblRowNumber") as Label).Text = (e.Row.RowIndex + 1).ToString();
}
}
}
Reply
Answers (
5
)
SQL Server 2005 query that uses the IIF with style date bool
sending mail from asp.net