ListView, image and SQL question - c#

I have the next code:
<asp:ListView ID="moreI"
runat="server"
DataSourceID="LinqDataSource1" onprerender="moreI_PreRender">
<ItemTemplate>
<asp:Image ID="Image1" Width="100px" Height="100px" runat="server" ImageUrl='<%# Eval("ImageUrl") %>' />
// and so on till the </ItemTemplate> and </asp:ListView>
I have the next method:
protected void checkTheImage()
{
foreach (ListViewItem item in moreI.Items)
{
((Image)item.FindControl("Image1")).ImageUrl = "noImage.jpg";
}
}
And the sql
protected Boolean ImageCheck()
{
SqlConnection connection = new SqlConnection(#"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\***.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
string CommandText2 = "SELECT noImage FROM Machreta WHERE noImage = 1";
SqlCommand command2 = new SqlCommand(CommandText2, connection);
connection.Open();
Boolean check = (Boolean)command2.ExecuteScalar();
connection.Close();
//here i stoped without the return.
I have the table Machreta with the column noImage (bit).
where the noImage = true, i want to show the noImage.jpg from the checkTheImage()
where the noImage = false, i want to show the '<%# Eval("ImageUrl") %>'
I have problem with the logic, maybe too many hours of work... what can you suggest me?

I think you need to update the datasource that bind to the listview to contain the boolean variable that specify if to use the ImageUrl or the noimage.jpeg
<asp:ListView ID="moreI"
runat="server"
DataSourceID="LinqDataSource1" onprerender="moreI_PreRender">
<ItemTemplate>
<asp:Image ID="Image1" Width="100px" Height="100px" runat="server" ImageUrl='<%# checkImage(Eval("ImageUrl"), Eval("noImage")) %>' />
// and so on till the </ItemTemplate> and </asp:ListView>
protected string checkTheImage(object ImageUrl, object noImage)
{
if((bool)noImage)
{
return "noImage.jpg";
}
else
{
return ImageUrl.ToString();
}
}

Related

Inserting image on datalist using image name from database

I have an issue trying to display image in my DataList from the database and have access to the database that I'm pulling my images from. But when I run the WebForm the image does not appear, what is wrong in my codes?
<asp:DataList ID="DataList1" runat="server" DataKeyField="dishID" DataSourceID="SqlDataSource1" BorderStyle="Solid" GridLines="Both" RepeatColumns="4" RepeatDirection="Horizontal" Width="1259px" >
<ItemTemplate>
<table class="auto-style1">
<tr><td>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "FoodPictures/" + Eval("dishImage") %>' />
This is how I'm retrieving from the database:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(_connStr);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from Dish";
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
conn.Close();
DataList1.DataSource = dt;
DataList1.DataBind();
conn.Close();
}
I think Your Page do not get image on proper path....
use
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "../FoodPictures/" + Eval("dishImage") %>' />
Or
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "~/FoodPictures/" + Eval("dishImage") %>' />
Try this..
Assuming if dishImage is a name like imagename.jpg stored in database then try Convert.ToString(Eval("dishImage")) like:
<asp:Image ID="Image1" ImageUrl='<% # "~/FoodPictures/" + Convert.ToString(Eval("dishImage")) %>' runat="server" />

How to dynamically add Id to a Textbox and retrive it in code behind in webforms

I am building an e-commerce website in ASP Webforms. I am displaying product list as listview which fetches data from SQL Serer. I have a textbox in the item template of the listview where a user can enter the quantity for the product. I want to set ID dynamically to the Control ID so I can retrieve the qty in code behind.
bio.aspx
<ItemTemplate>
<td runat="server" style="background-color:#DCDCDC;color: #000000;">
<asp:Image ImageUrl='<%# Eval("Image") %>' ID ="ImageLable" runat="server" CssClass="productImage" />
<br />
<asp:Label ID="Product_NameLabel" runat="server" Text='<%# Eval("[Product Name]") %>' />
<br />Price:
<asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("Price") %>' />
<br />
<asp:TextBox ID='TextBox1' runat="server"></asp:TextBox>
</br>
<asp:Button ID="ButtonClick" runat="server" Text="Add To Cart" CommandArgument='<%# Eval("Id") %>' OnClick="ButtonClick_Click"/>
</td>
</ItemTemplate>
bio.cs
protected void ButtonClick_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
string buttonId = button.CommandArgument.ToString();
int id = Convert.ToInt32(buttonId);
String connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["2016_675_z1787626ConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionString))
{
String command = "Insert into Cart (ProductId,ProductQty,UserId) Values(#ProductId,#ProductQ,1)";
conn.Open();
SqlCommand cmd = new SqlCommand(command, conn);
cmd.Parameters.Add("#ProductId", System.Data.SqlDbType.Int).Value = id;
cmd.Parameters.Add("#ProductQ", System.Data.SqlDbType.Int).Value = 1;
cmd.ExecuteNonQuery();
}
Response.Redirect("cart.aspx");
}
You want to use Button Control's Command Event (not click event), so that you can retrieve CommandArgument.
For example,
<asp:Button runat="server" OnCommand="AddToCartButton_Command"
CommandArgument='<%# Eval("Id") %>' ID="AddToCartButton"
Text="Add To Cart"/>
protected void AddToCartButton_Command(object sender, CommandEventArgs e)
{
var id = Convert.ToInt32(e.CommandArgument);
// Sibling textbox
var parent = (sender as Button).Parent;
var textbox = parent.FindControl("TextBox1") as TextBox;
}

Using Dropdown List for options for Search function in ASP.NET

I did work on a Search function where the user can type the item they want to search on a textbox, then hit a button to search.
the Search function is already working but for one option only (ContractNo), now I like to add a Dropdown list to add more options for the users to search (add: EmpID, TrainingCode etc)
here is the code for the aspx: (yes i only just included the codes that I think is necessary)
<div id="contentarea">
<p> Search Employee ID, Training Code, Contract Number<br/>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Contract Number</asp:ListItem>
<asp:ListItem>Training Code</asp:ListItem>
<asp:ListItem>Employee ID</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="searchText" runat="server" Height="16px" Width="146px"></asp:TextBox>
<asp:Button ID="ButtonSearch" runat="server" Text="Search" OnClick="ButtonSearch_Click" />
<asp:Button ID="ButtonClear" runat="server" Text="Clear" OnClick="ButtonClear_Click" />
</p>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="TrainingCode" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display." ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
</a>
</Itemtemplate>
</asp:TemplateField>--%>
<asp:TemplateField HeaderText="ContractNo" SortExpression="ContractNo">
<ItemTemplate>
<asp:Label ID="LabelContractNo" Text='<% #HighlightText(Eval("ContractNo").ToString()) %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TrainingCode" SortExpression="TrainingCode">
<ItemTemplate>
<asp:Label ID="LabelTrainingCode" runat="server" Text='<%# Eval("TrainingCode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EmpID" SortExpression="EmpID">
<ItemTemplate>
<asp:Label ID="LabelEmpID" runat="server" Text='<%# Eval("EmpID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ContractDate" SortExpression="ContractDate">
<ItemTemplate>
<asp:Label ID="LabelContractDate" runat="server" Text='<%# Eval("ContractDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ContractDuration" SortExpression="ContractDuration">
<ItemTemplate>
<asp:Label ID="LabelContractDuration" runat="server" Text='<%# Eval("ContractDuration") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ServiceStart" SortExpression="ServiceStart">
<ItemTemplate>
<asp:Label ID="LabelServiceStart" runat="server" Text='<%# Eval("ServiceStart") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ContractValue" SortExpression="ContractValue">
<ItemTemplate>
<asp:Label ID="LabelContractValue" runat="server" Text='<%# Eval("ContractValue") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Served" SortExpression="Served">
<ItemTemplate>
<asp:Label ID="LabelServed" runat="server" Text='<%# Bind("Served") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbx %>" DeleteCommand="DELETE FROM [SCHOLARSHIPCONTRACT] WHERE [TrainingCode] = #TrainingCode" InsertCommand="INSERT INTO [SCHOLARSHIPCONTRACT] ([ContractNo], [TrainingCode], [EmpID], [ContractDate], [ContractDuration], [ServiceStart], [ContractValue], [Served]) VALUES (#ContractNo, #TrainingCode, #EmpID, #ContractDate, #ContractDuration, #ServiceStart, #ContractValue, #Served)" SelectCommand="SELECT * FROM [SCHOLARSHIPCONTRACT]" UpdateCommand="UPDATE [SCHOLARSHIPCONTRACT] SET [ContractNo] = #ContractNo, [EmpID] = #EmpID, [ContractDate] = #ContractDate, [ContractDuration] = #ContractDuration, [ServiceStart] = #ServiceStart, [ContractValue] = #ContractValue, [Served] = #Served WHERE [TrainingCode] = #TrainingCode" FilterExpression="ContractNo '%{0}%'">
<FilterParameters>
<asp:ControlParameter Name="ContractNo" ControlID="searchText" PropertyName="Text"/>
</FilterParameters>
</asp:SqlDataSource>
and the FULL C# backend code:
private string SearchString = "";
protected void Page_Load(object sender, EventArgs e)
{
}
public string HighlightText(string InputText)
{
string Search_str = searchText.Text;
Regex RegExp = new Regex(Search_str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase);
return RegExp.Replace(InputText, new MatchEvaluator(ReplaceKeywords));
}
public string ReplaceKeywords(Match m) //this is just to highlight the item searched
{
return ("<span class=highlight>" + m.Value + "</span>");
}
protected void ButtonSearch_Click(object sender, EventArgs e)
{
SearchString = searchText.Text;
}
protected void ButtonClear_Click(object sender, EventArgs e)
{
searchText.Text = "";
SearchString = "";
GridView1.DataBind();
}
Now im thinking to add an if statement maybe? on the FilterParameters? like
<% if (DropDownList1.text == "EmpID"){}
but Im not sure.. Can you suggest other methods please?? Let me know if you might need other codes from the program. Thank you in advance
Try to use generic options. Use DropDown to list all types (EmpID, TrainingCode). On the basis of type selection use Switch to filter by entered text in TextBox.
SearchString = searchText.Text;
Switch(types)
{
case EmpID:
DoOperation();
break;
case TrainingCode:
DoOperation();
break;
}
Ok , here the solution, it's working ok in my computer, I hope this is what you want to achieve. I put some comments in the code, read them
in aspx page , put this code
<div id="contentarea">
<p> Search Employee ID, Training Code, Contract Number<br/>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="ContractNo">Contract Number</asp:ListItem>
<asp:ListItem Value="TrainingCode">Training Code</asp:ListItem>
<asp:ListItem Value="EmpID">Employee ID</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="searchText" runat="server" Height="16px" Width="146px"></asp:TextBox>
<asp:Button ID="ButtonSearch" runat="server" Text="Search" OnClick="ButtonSearch_Click" />
<asp:Button ID="ButtonClear" runat="server" Text="Clear" OnClick="ButtonClear_Click" />
</p>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</div>
and this is the C# code in aspx.cs
private string SearchString = "";
protected void Page_Load(object sender, EventArgs e)
{
}
public string HighlightText(string InputText)
{
string Search_str = searchText.Text;
Regex RegExp = new Regex(Search_str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase);
return RegExp.Replace(InputText, new MatchEvaluator(ReplaceKeywords));
}
public string ReplaceKeywords(Match m) //this is just to highlight the item searched
{
return ("<span class=highlight>" + m.Value + "</span>");
}
private DataTable GetData(string query)
{
// Read connection string from web.config file , Important, change the
//ConnectionStrings name here (testConnectionString)
// and replace it with your connection name , you can find it in web.config file
// in <connectionStrings> tag , you find it after this tag <add name="
string CS = ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlDataAdapter sda = new SqlDataAdapter(query, con);
using (DataTable dt = new DataTable())
{
con.Open();
sda.Fill(dt);
return dt;
}
}
}
protected void ButtonSearch_Click(object sender, EventArgs e)
{
SearchString = searchText.Text;
string columnName = DropDownList1.SelectedValue;
string searchSQL = "SELECT * FROM [SCHOLARSHIPCONTRACT] WHERE " + columnName + "= '" + SearchString + "'";
//bind the SCHOLARSHIPCONTRACT table data into GridView1
DataTable dt = this.GetData(searchSQL);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void ButtonClear_Click(object sender, EventArgs e)
{
searchText.Text = "";
SearchString = "";
GridView1.DataBind();
}

Updation Not Done inside Gridbox. Exception Handling Error

I have did a Edit/Update/Delete/Cancel in the gridbox. All the functionlaities are working fine. Except Updating.
When I Click, I get an error stating,
NullReferenceException was unhandled by the user code.
Object Reference not set to an instance of an object
Here is the Code for updating the data's
protected void Show_Grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = int.Parse(Show_Grid.DataKeys[e.RowIndex].Value.ToString());
TextBox title_txt = (TextBox)Show_Grid.Rows[e.RowIndex].FindControl("Title");
TextBox Desc_Txt = (TextBox)Show_Grid.Rows[e.RowIndex].FindControl("Description");
DropDownList Prior_Drop = (DropDownList)Show_Grid.Rows[e.RowIndex].FindControl("Priority");
Update_todo(id, title_txt.Text, Desc_Txt.Text, Prior_Drop.SelectedValue);
Show_Grid.EditIndex = -1;
BindData();
}
private void Update_todo(int id, string title, string desc, string prior)
{
string source = "Data Source=.\\SQLEXPRESS;AttachDbFilename=...//...//..//..//tododb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection dbconnect = new SqlConnection(source);
string query = "UPDATE todolist SET Title='" + title + "', Description='" + desc + "', Priority='" + prior + "' WHERE id =" + id + " ";
SqlCommand cmd = new SqlCommand(query, dbconnect);
dbconnect.Open();
cmd.ExecuteNonQuery();
}
In the Gridbox, while editing i gave TextBox with SingleLine for Title, TextBox with Multiline for Description & DropDown for Priority.
I get the error in this line
Update_todo(id, title_txt.Text, Desc_Txt.Text, Prior_Drop.SelectedValue);
Grid view markup
<asp:TemplateField HeaderText="Description">
<EditItemTemplate>
<asp:TextBox ID="Desc_Txt" runat="server" Text='<%# Eval("Description") %>'
TextMode="MultiLine"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Description") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Priority">
<EditItemTemplate>
<asp:DropDownList ID="Prior_Drop" runat="server"
SelectedValue='<%# Eval("Priority") %>'>
<asp:ListItem></asp:ListItem>
<asp:ListItem>High</asp:ListItem>
<asp:ListItem>Medium</asp:ListItem>
<asp:ListItem>Low</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("Priority") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Operation" ShowDeleteButton="True"
ShowEditButton="True" />
</Columns>
</asp:GridView>
You are either not getting one or all of the controls title_txt, Desc_Txt or Prior_Drop
Check if they are null or not before accessing them
You can have a check before calling update like this, however you have to make sure that it does not break your functionality in any way
if(title_txt!=null && Desc_Text!=null && Prior_Drop!=null)
{
Update_todo(id, title_txt.Text, Desc_Txt.Text, Prior_Drop.SelectedValue);
Show_Grid.EditIndex = -1;
BindData();
}
Update
The mistake you are doing is, you are accessing the controls with wrong ids, use this and it will work
TextBox title_txt = (TextBox)Show_Grid.Rows[e.RowIndex].FindControl("title_txt");
TextBox Desc_Txt = (TextBox)Show_Grid.Rows[e.RowIndex].FindControl("Desc_Txt");
DropDownList Prior_Drop = (DropDownList)Show_Grid.Rows[e.RowIndex].FindControl("Prior_Drop");

Update DataGridView with a DropDownList

I have a problem to update a datagridview when modifications are made.
To update a column ("Criticite"), I implemented a DropDownList containing 2 choices. I can select a choice, but I click on the "Update" button none change is done.
Could you please help me on this subject ?
Thanks a lot !
Here is the C# code :
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;
using System.IO.Compression;
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindData();
}
}
private void BindData()
{
string query = "select ID,Client,Entite,Date,Source,Domaine,Categorie,SousCategorie,Criticite,Contenu from Results";
SqlCommand cmd = new SqlCommand(query);
GridView1.DataSource = GetData(cmd);
GridView1.DataBind();
}
private DataTable GetData(SqlCommand cmd)
{
string strConnString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
protected void EditCustomer(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindData();
}
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Edit)
{
DropDownList drpcategory1 = (DropDownList)e.Row.FindControl("DropDownList1");
String query = "Select Criticite from Results";
SqlCommand cmd = new SqlCommand(query);
DataTable dt = GetData(cmd);
drpcategory1.DataSource = dt;
drpcategory1.DataTextField = "Criticite";
drpcategory1.DataValueField = "Criticite";
drpcategory1.DataBind();
}
}
private void BindData(string Query)
{
string connectionstring = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionstring))
{
conn.Open();
using (SqlCommand comm = new SqlCommand(Query + ";select ID,Client,Entite,Date,Source,Domaine,Categorie,SousCategorie,Criticite,Contenu from Results", conn))
{
SqlDataAdapter da = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
protected void RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label ID = GridView1.Rows[e.RowIndex].FindControl("ID") as Label;
TextBox Entite = GridView1.Rows[e.RowIndex].FindControl("txtEntite") as TextBox;
TextBox Date = GridView1.Rows[e.RowIndex].FindControl("txtDate") as TextBox;
TextBox Source = GridView1.Rows[e.RowIndex].FindControl("txtSource") as TextBox;
TextBox Domaine = GridView1.Rows[e.RowIndex].FindControl("txtDomaine") as TextBox;
TextBox Categorie = GridView1.Rows[e.RowIndex].FindControl("txtCategorie") as TextBox;
TextBox SousCategorie = GridView1.Rows[e.RowIndex].FindControl("txtSousCategorie") as TextBox;
TextBox Criticite = GridView1.Rows[e.RowIndex].FindControl("lblCriticite") as TextBox;
TextBox Contenu = GridView1.Rows[e.RowIndex].FindControl("txtContenu") as TextBox;
String UpdateQuery = string.Format("UPDATE Results SET Entite='{0}',Date='{1}',Source='{2}',Domaine='{3}',Categorie='{4}',SousCategorie='{5}',Criticite='{6}',Contenu='{7}' WHERE ID = {8}", Entite.Text, Date.Text, Source.Text, Domaine.Text, Categorie.Text, SousCategorie.Text, Criticite.Text, Contenu.Text, ID.Text);
GridView1.EditIndex = -1;
BindData(UpdateQuery);
}
}
And here the aspx default page :
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
</asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<p>
Début</p>
<p>
</p>
<p>
</p>
<asp:GridView ID="GridView1" runat="server" GridLines="None" AutoGenerateColumns="false"
AlternatingRowStyle-BackColor="#EEEEEE" EditRowStyle-BorderColor="Red"
OnRowEditing = "EditCustomer"
OnRowDataBound = "RowDataBound"
OnRowUpdating = "RowUpdating"
OnRowCancelingEdit = "CancelEdit">
<Columns>
<asp:TemplateField Visible="false" HeaderText="ID">
<ItemTemplate>
<asp:Label runat="server" ID="ID" Text='<%#Eval("ID")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Client">
<ItemTemplate>
<asp:Label runat="server" ID="Client" Text='<%#Eval("Client") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtClient" Text='<%#Eval("Client") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Entite">
<ItemTemplate>
<asp:Label runat="server" ID="Entite" Text='<%#Eval("Entite") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtEntite" Text='<%#Eval("Entite") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:Label runat="server" ID="Date" Text='<%#Eval("Date") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtDate" Text='<%#Eval("Date") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Source">
<ItemTemplate>
<asp:Label runat="server" ID="Source" Text='<%#Eval("Source") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtSource" Text='<%#Eval("Source") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Domaine">
<ItemTemplate>
<asp:Label runat="server" ID="Domaine" Text='<%#Eval("Domaine") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtDomaine" Text='<%#Eval("Domaine") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Categorie">
<ItemTemplate>
<asp:Label runat="server" ID="Categorie" Text='<%#Eval("Categorie") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtCategorie" Text='<%#Eval("Categorie") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SousCategorie">
<ItemTemplate>
<asp:Label runat="server" ID="SousCategorie" Text='<%#Eval("SousCategorie") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtSousCategorie" Text='<%#Eval("SousCategorie") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Criticite">
<ItemTemplate>
<asp:Label runat="server" ID="lblCriticite" Text='<%#Eval("Criticite") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="lblCriticite" Text='<%#Eval("Criticite") %>' Visible = "false" />
<asp:DropDownList ID="DropDownList1" runat = "server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Contenu">
<ItemTemplate>
<asp:Label runat="server" ID="Contenu" Text='<%#Eval("Contenu") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtContenu" Text='<%#Eval("Contenu") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" />
<asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Content>
Change the following line
TextBox Criticite = GridView1.Rows[e.RowIndex].FindControl("lblCriticite") as TextBox;
into:
DropDownList Criticite = GridView1.Rows[e.RowIndex].FindControl("DropDownList1") as DropDownList;
and use Criticite.SelectedValue instead of Criticite.Text.
Also, your update query is at risk of SQL injection attacks.

Categories