Hi georgeacuster,
Your Image button is outside the gridview becouse of that you are getting the error.
You need to take the image button inside the gridview.
Please refer below sample.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkselect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CustomerId" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:DropDownList runat="server" ID="ddlCountry"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgbtn" runat="server" OnClick="OnUpdate" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView><br /><br />
<asp:Label ID="lblName" runat="server">Name</asp:Label>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GridView();
}
}
protected void OnUpdate(object sender, EventArgs e)
{
ImageButton imgbtn = (ImageButton)sender;
GridViewRow GridView1Row = (GridViewRow)imgbtn.NamingContainer;
DropDownList ddl = (DropDownList)(GridView1Row.Cells[3].Controls[0].FindControl("ddlCountry"));
string t_ddl = ddl.Text;
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chkSelect = (CheckBox)row.FindControl("chkSelect");
if (chkSelect.Checked && !String.IsNullOrEmpty(t_ddl.ToString()))
{
int id = int.Parse(row.Cells[1].Text);
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("UPDATE Customers SET Name=@Name WHERE CustomerId=@CustomerId", con))
{
con.Open();
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@CustomerId", id);
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
this.GridView();
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con))
{
con.Open();
DropDownList ddlCountry = (e.Row.FindControl("ddlCountry") as DropDownList);
ddlCountry.DataTextField = "Country";
ddlCountry.DataValueField = "CustomerId";
ddlCountry.DataSource = cmd.ExecuteReader();
ddlCountry.DataBind();
con.Close();
}
}
}
}
private void GridView()
{
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.GridView()
End If
End Sub
Protected Sub OnUpdate(ByVal sender As Object, ByVal e As EventArgs)
Dim imgbtn As ImageButton = CType(sender, ImageButton)
Dim GridView1Row As GridViewRow = CType(imgbtn.NamingContainer, GridViewRow)
Dim ddl As DropDownList = CType((GridView1Row.Cells(3).Controls(0).FindControl("ddlCountry")), DropDownList)
Dim t_ddl As String = ddl.Text
For Each row As GridViewRow In GridView1.Rows
Dim chkSelect As CheckBox = CType(row.FindControl("chkSelect"), CheckBox)
If chkSelect.Checked AndAlso Not String.IsNullOrEmpty(t_ddl.ToString()) Then
Dim id As Integer = Integer.Parse(row.Cells(1).Text)
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand("UPDATE Customers SET Name=@Name WHERE CustomerId=@CustomerId", con)
con.Open()
cmd.Parameters.AddWithValue("@Name", txtName.Text)
cmd.Parameters.AddWithValue("@CustomerId", id)
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End If
Next
Me.GridView()
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con)
con.Open()
Dim ddlCountry As DropDownList = (TryCast(e.Row.FindControl("ddlCountry"), DropDownList))
ddlCountry.DataTextField = "Country"
ddlCountry.DataValueField = "CustomerId"
ddlCountry.DataSource = cmd.ExecuteReader()
ddlCountry.DataBind()
con.Close()
End Using
End Using
End If
End Sub
Private Sub GridView()
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Using
End Using
End Using
End Sub
Screenshot