error in finding control in gridview - c#

I had gridview which on databound I need to hid Image control so I did this code but error apear( grid view dosenot containe defination of RowIndex) when i tried to find control
C# Code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Img = GridView1.Rows[e.RowIndex].FindControl("Logo") as Image ;
using (SqlConnection con = Connection.GetConnection())
{
string Sql = "Select Logo From Model where Id=#Id";
SqlCommand com = new SqlCommand(Sql, con);
com.Parameters.Add(Parameter.NewInt("#Id", DDLModel.SelectedValue));
com.CommandType = CommandType.Text;
SqlDataReader dr = com.ExecuteReader();
if (dr.Read())
{
string Img2 = dr["Logo"].ToString();
if (Img2 == System.DBNull.Value.ToString())
{
Img.Visible = false;
}
}
}
}

GridViewRowEventArgs contains the row so you could try:
image = e.Row.FindControl("Logo") as Image;

Related

Asp.Net finding in Code-behind edited values in GridView with autogenerated columns

I am trying to create a user control of a gridview that will be able to display, edit and delete records, being bound to any datatable.
In my user control I have:
<asp:GridView ID="EditableGrid" runat="server" Width="500px" Height="500px" AllowSorting="True"
AutoGenerateColumns = "true"
AutoGenerateDeleteButton ="true" AutoGenerateEditButton="true"
OnRowEditing="EditableGrid_RowEditing"
OnRowCancelingEdit="EditableGrid_RowCancelingEdit"
OnRowUpdating="EditableGrid_RowUpdating"
OnRowDeleting="EditableGrid_RowDeleting"
></asp:GridView>
In my code behind I have:
public void InitGrid(string theconnstr, string thetablename)
{
connstr = theconnstr;
tablename = thetablename;
// Session["edgconnstr"] = connstr;
// Session["edgtablename"] = tablename;
con = new SqlConnection(connstr);
con.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandText = "SELECT * FROM " + tablename;
using (SqlDataReader rd = cmd.ExecuteReader())
{
if (!rd.HasRows) return;
fields = new List<EdField>();
for (int i =0; i < rd.FieldCount; i++)
{
fields.Add(new EdField(rd.GetName(i), rd.GetDataTypeName(i)));
}
}
}
con.Close();
}
public void Bind()
{
// connstr = (String)Session["edgconnstr"];
// tablename = (String)Session["edgtablename"];
con = new SqlConnection(connstr);
con.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandText = "SELECT * FROM " + tablename;
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
da.Fill(dt);
EditableGrid.DataSource = dt;
EditableGrid.DataBind();
EditableGrid.Visible = true;
}
}
}
con.Close();
}
protected void EditableGrid_RowEditing(object sender, GridViewEditEventArgs e)
{
EditableGrid.EditIndex = e.NewEditIndex;
Bind();
}
protected void EditableGrid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
EditableGrid.EditIndex = -1;
Bind();
}
protected void EditableGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
}
protected void EditableGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
}
Now I have tried with no success to find the new values of the edited row in the EditableGrid_RowUpdating event handler. I just do not have access to the textboxes, and therefore I cannot run the query to update the old values to the new value. Is what I want to achieve even possible?
Use the e.NewValues Collection.
protected void EditableGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int tx = Convert.ToInt32(e.NewValues[0]);
}

update button on nested gridview asp.net c#

I have this nested grid view in which the data are shown in textboxes and i also have an update button but when i click the button instead of updating the data on the data table it just updates the new value on the grid view itself and shows it along with the old value and i don't get any error though. Please help me thanks.
the code:
private static DataTable GetData(string query)
{
string strConnString = ConfigurationManager.ConnectionStrings["MandapamDatabase"].ConnectionString;
using (OleDbConnection con = new OleDbConnection(strConnString))
{
using (OleDbCommand cmd = new OleDbCommand())
{
cmd.CommandText = query;
using (OleDbDataAdapter sda = new OleDbDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string ID = gvSDate.DataKeys[e.Row.RowIndex].Value.ToString();
GridView gvSDate2 = e.Row.FindControl("gvSDate2") as GridView;
gvSDate2.DataSource = GetData(string.Format("select ID, Function, FunctionDate, FunctionTime, CelebrateName from function where ID={0}", ID));
gvSDate2.DataBind();
}
}
protected void gvSDate2_RowDataBound(object sender, GridViewRowEventArgs f)
{
if (f.Row.RowType == DataControlRowType.DataRow)
{
GridView gvSDate2 = f.Row.FindControl("gvSDate2") as GridView;
TextBox textFunction = f.Row.FindControl("textFunction") as TextBox;
TextBox textFunctionDate = f.Row.FindControl("textFunctionDate") as TextBox;
TextBox textFunctionTime = f.Row.FindControl("textFunctionTime") as TextBox;
TextBox textCelebrateName = f.Row.FindControl("textCelebrateName") as TextBox;
Label lblID = f.Row.FindControl("lblID") as Label;
Button update1 = f.Row.FindControl("update1") as Button;
string Function = textFunction.Text;
string FunctionDate = textFunctionDate.Text;
string FunctionTime = textFunctionTime.Text;
string CelebrateName = textCelebrateName.Text;
string ID1 = lblID.Text;
update1.Click += (send, g) =>
{
string connString = ConfigurationManager.ConnectionStrings["MandapamDatabase"].ConnectionString;
OleDbConnection connection = new OleDbConnection(connString);
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string updateQuery = "UPDATE function SET Function=#Function, FunctionDate=#FunctionDate, FunctionTime=#FunctionTime, CelebrateName=#CelebrateName WHERE ID=#ID";
command.CommandType = CommandType.Text;
command.CommandText = updateQuery;
command.Parameters.AddWithValue("#ID", ID1);
command.Parameters.AddWithValue("#Function", Function);
command.Parameters.AddWithValue("#FunctionDate", FunctionDate);
command.Parameters.AddWithValue("#FunctionTime", FunctionTime);
command.Parameters.AddWithValue("#CelebrateName", CelebrateName);
};
}
}

Convert wpf datagrid to asp gridview

This c# code works fine in wpf framework.
Now I need this code in asp.net.
I am using GridView instead of DataGrid.
What changes do I have to do?
Here is my c# code:
DataRowView SelectedRowValue = (DataRowView)dataGrid1.SelectedValue;
byte[] ImageBytes = (byte[])SelectedRowValue.Row.ItemArray[1];
MySqlCommand cmd2 = new MySqlCommand("INSERT INTO Images (Image) VALUES (#ImageSource)", con);
cmd2.Parameters.Add("#ImageSource", MySqlDbType.Blob, ImageBytes.Length).Value = ImageBytes;
cmd2.ExecuteNonQuery();
You can use Grid_RowUpdating Event using c#
protected void oGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lblID = (Label)row.FindControl("Label1");
TextBox txtImage = (TextBox)row.Cells[0].Controls[0];
//........
Then Update Command for MySQl which You are using//
MySqlCommand oMySqlCommand = connection.CreateCommand();
oMySqlCommand.CommandText = ""update detail set
Image='"+txtImage.Text+"'where id='"+userid+"'";
connection.Open();
command.ExecuteNonQuery();
//.......
etc//
GridView1.EditIndex = -1;
}

Deleting is not supported by data source 'SqlDataSource1' in c#

This is my cs:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
SqlConnection con = Connection.DBconnection();
if (e.CommandName == "EditRow")
{
GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;
Textid.Text = gr.Cells[0].Text;
Textusername.Text = gr.Cells[1].Text;
Textclass.Text = gr.Cells[2].Text;
Textsection.Text = gr.Cells[3].Text;
Textaddress.Text = gr.Cells[4].Text;
}
else if (e.CommandName == "Deleterow")
{
GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;
SqlCommand com = new SqlCommand("StoredProcedure4", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("#ID", gr.Cells[0].Text);
var id = Int32.Parse(e.CommandArgument.ToString());
GridView1.DeleteRow(id);
com.ExecuteNonQuery();
}
}
I created student details form, I want to delete the row from gridview.
When i click delete, it shows following error:
Deleting is not supported by data source 'SqlDataSource1' unless DeleteCommand is specified.
I'm new to .net. Now getting started,
May i know, what is my mistake in the above code?
Thanks,
else if (e.CommandName == "Deleterow")
{
GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;
SqlCommand com = new SqlCommand("StoredProcedure4", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("#ID", gr.Cells[0].Text);
var id = Int32.Parse(e.CommandArgument.ToString());
//comment or remove below line
//GridView1.DeleteRow(id);
com.ExecuteNonQuery();
//rebind the data after delete
GridView1.DataBind();
}

How to enable all textbox controls in itemtemplate of Repeater by itemCommand?

I have a repeater and I used label for display and a hidden text box which holds the same value. Now I want to enable the text box so that the user can edit inputs in the table/repeater. Can anyone help me? below is my code
public void FillTable()
{
DataTable table = new DataTable();
using (MySqlConnection conn = Functions.GetConnection())
{
string sql = "SELECT FirstName, LastName from tbluser LIMIT 15";
using (MySqlCommand cmd = new MySqlCommand(sql, conn))
{
MySqlDataAdapter da = new MySqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(table);
conn.Close();
}
}
asd.DataSource = table;
asd.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
asd.Visible = true;
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
RepeaterItem item = e.Item;
Literal lblname = (Literal)item.FindControl("Label2");
TextBox txtbox = (TextBox)item.FindControl("Label3");
if (e.CommandName == "EDIT")
{
lblname.Visible = false;
txtbox.Visible = true;
You are not getting the textbox but the label twice:
TextBox txtbox = (TextBox)item.FindControl("Label3");

Categories