Mike Simo

Mike Simo

  • NA
  • 17
  • 12.5k

I got sick with editing and delete the datalist

May 21 2014 9:53 PM
Hi All
 
Please please i got really sick with trying many days to know how to fix editing and delete and update my datalist please if you are able to re-coding my code i will be thankful i have many links to explain how to edit datalist so i am not looking for any reference i just need some one to help me with re-coding my datalist controls.
 
Here is my datalist asp code:
 
<asp:DataList ID="DataList1" runat="server"
DataSourceID="SqlDataSourceUPAddAdsCate"
oneditcommand="DataList1_EditCommand1" >
<ItemTemplate>
<div class="updateicon">
<asp:ImageButton ID="Update" runat="server" CommandName="UpdateCommand"
ImageUrl="~/iconsimg/update.png" />
&nbsp;
<asp:ImageButton ID="Delete" runat="server"
ImageUrl="~/iconsimg/delete.png" />
</div>
<br />
<asp:Label ID="adstitledetailsshow" runat="server"
style="font-weight: 700; font-size: large; color: #3366FF; font-family: 'Times New Roman', Times, serif;" Text='<%# Eval("AdsTit") %>'></asp:Label>
<br /><br />
<table class="table">
<tr>
<td style="background-color: #f0f0f0" class="style30"><strong style="color: #333333">Ads Number :</strong></td>
<td class="style31">
<asp:Label ID="Label1" runat="server" Text='<%# Eval ("AdsID") %>'></asp:Label>
</td>
<td style="background-color: #f0f0f0" class="style32">
<strong style="color: #333333">Category :</strong></td>
<td class="style12">
<asp:Label ID="Label2" runat="server" Text='<%# Eval ("Section") %>'></asp:Label>
&nbsp;-
<asp:Label ID="Label4" runat="server" Text='<%# Eval ("Category") %>'></asp:Label>
</td>
</tr>
<tr>
<td style="background-color: #FFFFCC" class="style30">
<strong style="color: #333333">Price :</strong></td>
<td class="style31">
<asp:Label ID="Label5" runat="server" style="color: #CC0000" Text='<%# Eval("AdsPrice") %>'></asp:Label>
</td>
</tr>
<tr>
<td style="background-color: #f0f0f0" class="style30"><strong style="color: #333333">Location :</strong></td>
<td class="style31">
<asp:Label ID="Label6" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
&nbsp;-
<asp:Label ID="Label7" runat="server" Text='<%# Eval("State") %>'></asp:Label>
&nbsp;-
<asp:Label ID="Label8" runat="server" Text='<%# Eval("City") %>'></asp:Label>
</td>
</tr>
<tr>
<td style="background-color: #FFFFCC" class="style30">
<strong style="color: #333333">Keywords:</strong></td>
<td>
<asp:Label ID="wtaglbl" runat="server" Text='<%# Eval("Wtags") %>'></asp:Label>
</td>
</tr>
</table>
<br />
<h3 class="style14">Description :</h3>
<asp:Label ID="Label9" runat="server" Text='<%#Eval ("AdsDesc") %>'
style="text-align: justify"></asp:Label>
&nbsp;<br /> <br /><br />
</ItemTemplate>
<AlternatingItemTemplate>
</div>
</AlternatingItemTemplate>
<EditItemTemplate>
<div class="updateicon">
<asp:ImageButton ID="Updatemupdtebtn" runat="server" CommandName="UpdateCommand"
ImageUrl="~/iconsimg/update.png" />
&nbsp;
<asp:ImageButton ID="cancelbtn" runat="server" CommandName="Cancel"
ImageUrl="~/iconsimg/undo.png" />
</div>
<table>
<tr>
<td>Section:</td>
<td>
<asp:DropDownList ID="Catedrdoads" runat="server" AutoPostBack="True"
onselectedindexchanged="Catedrdoads_SelectedIndexChanged">
</asp:DropDownList></td>
</tr>
<tr>
<td>Category:</td>
<td>
<asp:DropDownList ID="SubCatedrdoads" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="SubCatedrdoads_SelectedIndexChanged">
</asp:DropDownList></td>
</tr>
<tr>
<td>Country:</td>
<td>
<asp:DropDownList ID="countrdrdolst" runat="server" AutoPostBack="True"
onselectedindexchanged="countrdrdolst_SelectedIndexChanged">
</asp:DropDownList></td></tr>
<tr>
<td>State:</td>
<td>
<asp:DropDownList ID="statedrdolst" runat="server" AutoPostBack="True"
Height="16px" onselectedindexchanged="statedrdolst_SelectedIndexChanged">
</asp:DropDownList></td>
</tr>
<tr>
<td>City:</td>
<td>
<asp:DropDownList ID="citiesdrdolst" runat="server" AutoPostBack="True"
onselectedindexchanged="citiesdrdolst_SelectedIndexChanged">
</asp:DropDownList></td></tr>
<tr>
<td>Ads title:</td>
<td>
<asp:TextBox ID="Adstitleedit" runat="server" Text='<%# Eval ("AdsTit") %>'></asp:TextBox></td></tr>
<tr>
<td>Ads Price:</td>
<td>
<asp:TextBox ID="PriceUpdate" runat="server" Text='<%# Eval ("AdsPrice") %>'></asp:TextBox></td></tr>
<tr>
<td>Description:</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Text= '<%# Eval ("AdsDesc") %>'
Height="85px" Width="350px" TextMode="MultiLine" Rows="12"></asp:TextBox></td></tr>
<tr>
<td>Search Keywords:</td>
<td>
<asp:TextBox ID="Tagupdate" runat="server" Text= '<%# Eval ("Wtags") %>'></asp:TextBox></td>
</tr>
</table>
<br />
<br />
</EditItemTemplate>
</asp:DataList>
 
And here is my behind code: 
 
 
namespace beravoSV
{
public partial class User_panel : System.Web.UI.Page
{
string sc = ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString.ToString();
protected void Page_Load(object sender, EventArgs e)
{
USRNMElbl.Text = Session["UsrNme"].ToString();
if (!Page.IsPostBack)
{
DataTable countrycascd = new DataTable();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString))
{
SqlDataAdapter adaptar = new SqlDataAdapter("select [countryID],[country] FROM [countr]", con);
adaptar.Fill(countrycascd);
countrdrdolst.DataSource = countrycascd;
countrdrdolst.DataTextField = "country";
countrdrdolst.DataValueField = "countryID";
countrdrdolst.DataBind();
}
countrdrdolst.Items.Insert(0, new ListItem("Välj land", "0"));
}
if (!Page.IsPostBack)
{
DataTable Sectiondt = new DataTable();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString))
{
SqlDataAdapter adaptar = new SqlDataAdapter("select [CateID],[Category] FROM [Section]", con);
adaptar.Fill(Sectiondt);
Catedrdoads.DataSource = Sectiondt;
Catedrdoads.DataTextField = "Category";
Catedrdoads.DataValueField = "CateID";
Catedrdoads.DataBind();
}
Catedrdoads.Items.Insert(0, new ListItem("Select Section", "0"));
}
BIND();
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
BIND();
}
private void BIND()
{
if (!IsPostBack)
{
SqlConnection conedit = new SqlConnection(sc);
SqlDataAdapter daedit = new SqlDataAdapter("Select AdsID,UID,Category,Country,State,City,Ad,AdsTit,AdsDesc,AdsPrice,Img1,img2,img3,img4,img5,Wtags,AdsDate from Ads WHERE ([UsrNme] = @UID)", conedit);
DataSet dsedit = new DataSet();
conedit.Open();
daedit.Fill(dsedit);
conedit.Close();
DataList1.DataSource = daedit.Tables[0];
DataList1.DataBind();
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = 0;
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = 1;
}
protected void LinkButton3_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = 2;
}
protected void LinkButton4_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = 3;
}
protected void LinkButton5_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = 4;
}
protected void LinkButton6_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = 5;
}
protected void addadsbtn_Click(object sender, EventArgs e)
{
Guid newGUID = Guid.NewGuid();
SqlConnection cn = new SqlConnection(sc);
SqlCommand cmd = new SqlCommand();
string sqlstatment = "INSERT INTO [ads] ([Section], [Category], [UID], [AdsTit], [AdsDesc], [Country], [State], [City], [AdsPrice], [Img1], [img2], [img3], [img4], [img5], [Wtags]) VALUES (@Section, @Category, @UID, @AdsTit, @AdsDesc, @Country, @State, @City, @AdsPrice, @Img1, @img2, @img3, @img4, @img5, @Wtags)";
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlstatment;
//Insert the parameters first
cmd.Parameters.AddWithValue("@Section", Catedrdoads.SelectedItem.Text);
cmd.Parameters.AddWithValue("@Category", SubCatedrdoads.SelectedItem.Text);
cmd.Parameters.AddWithValue("@UID", USRNMElbl.Text);
cmd.Parameters.AddWithValue("@AdsTit", addadstittxtbx.Text);
cmd.Parameters.AddWithValue("@AdsDesc", TextArea1.InnerText);
cmd.Parameters.AddWithValue("@Country", countrdrdolst.SelectedItem.Text);
cmd.Parameters.AddWithValue("@State", statedrdolst.SelectedItem.Text);
cmd.Parameters.AddWithValue("@City", citiesdrdolst.SelectedItem.Text);
cmd.Parameters.AddWithValue("@AdsPrice", adsaddpristxtbx.Text);
cmd.Parameters.AddWithValue("@Img1", FileUploadImg1.FileName);
cmd.Parameters.AddWithValue("@Img2", FileUploadImg2.FileName);
cmd.Parameters.AddWithValue("@Img3", FileUploadImg3.FileName);
cmd.Parameters.AddWithValue("@Img4", FileUploadImg4.FileName);
cmd.Parameters.AddWithValue("@Img5", FileUploadImg5.FileName);
cmd.Parameters.AddWithValue("@Wtags", addadswtagtxtbtn.Text);
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ad.SelectCommand = cmd;
ad.Fill(ds);
Response.Redirect("User panel.aspx");
}
protected void countrdrdolst_SelectedIndexChanged(object sender, EventArgs e)
{
int countryID = Convert.ToInt32(countrdrdolst.SelectedValue);
DataTable StateDT = new DataTable();
using (SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString))
{
SqlDataAdapter adaptar = new SqlDataAdapter("Select [StateID],[State] FROM [State] WHERE [countryID] = " + countryID, con2);
adaptar.Fill(StateDT);
statedrdolst.DataSource = StateDT;
statedrdolst.DataTextField = "State";
statedrdolst.DataValueField = "StateID";
statedrdolst.DataBind();
}
statedrdolst.Items.Insert(0, new ListItem("Välj Län", "0"));
}
protected void statedrdolst_SelectedIndexChanged(object sender, EventArgs e)
{
int StateID = Convert.ToInt32(statedrdolst.SelectedValue);
DataTable Citydt = new DataTable();
using (SqlConnection con3 = new SqlConnection(ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString))
{
SqlDataAdapter adaptar = new SqlDataAdapter("select [CityID],[City] FROM [Cities] WHERE [StateID] = " + StateID, con3);
adaptar.Fill(Citydt);
citiesdrdolst.DataSource = Citydt;
citiesdrdolst.DataTextField = "City";
citiesdrdolst.DataValueField = "CityID";
citiesdrdolst.DataBind();
}
citiesdrdolst.Items.Insert(0, new ListItem("Välj Stad", "0"));
}
protected void citiesdrdolst_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Catedrdoads_SelectedIndexChanged(object sender, EventArgs e)
{
int CateID = Convert.ToInt32(Catedrdoads.SelectedValue);
DataTable SubCatedt = new DataTable();
using (SqlConnection con4 = new SqlConnection(ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString))
{
SqlDataAdapter adaptar = new SqlDataAdapter("Select [SubcateID],[Subcateg] FROM [SubCate] WHERE [CateID] = " + CateID, con4);
adaptar.Fill(SubCatedt);
SubCatedrdoads.DataSource = SubCatedt;
SubCatedrdoads.DataTextField = "Subcateg";
SubCatedrdoads.DataValueField = "SubcateID";
SubCatedrdoads.DataBind();
}
SubCatedrdoads.Items.Insert(0, new ListItem("Select Category", "0"));
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
TextBox addadstittxtbxedit = e.Item.FindControl("addadstittxtbxedit") as TextBox;
TextBox pricetxtboxedit = e.Item.FindControl("pricetxtboxedit") as TextBox;
DropDownList Catedrdoadsedit = e.Item.FindControl("Catedrdoadsedit") as DropDownList;
DropDownList SubCatedrdoadsedit = e.Item.FindControl("SubCatedrdoadsedit") as DropDownList;
DropDownList countrdrdolstedit = e.Item.FindControl("countrdrdolstedit") as DropDownList;
DropDownList statedrdolstedit = e.Item.FindControl("statedrdolstedit") as DropDownList;
DropDownList citiesdrdolstedit = e.Item.FindControl("citiesdrdolstedit") as DropDownList;
TextBox destxtboxedit = e.Item.FindControl("destxtboxedit") as TextBox;
SqlConnection conedit = new SqlConnection(sc);
SqlCommand cmdinst = new SqlCommand();
cmdinst.Connection = conedit;
cmdinst.CommandText = " insert into ads (Section,Category,Country,State,City,AdsTit,AdsPrice,AdsDesc) values(@Section,@Category,@Country,@State,@City,@AdsTit,@AdsPrice,@AdsDesc)";
cmdinst.Parameters.Add("@Section", SqlDbType.NVarChar, 50).Value = SubCatedrdoadsedit.SelectedValue;
cmdinst.Parameters.Add("@Category", SqlDbType.NVarChar, 50).Value = Catedrdoadsedit.SelectedValue;
cmdinst.Parameters.Add("@Country", SqlDbType.NVarChar, 50).Value = countrdrdolstedit.SelectedValue;
cmdinst.Parameters.Add("@State", SqlDbType.NVarChar, 50).Value = statedrdolstedit.SelectedValue;
cmdinst.Parameters.Add("@City", SqlDbType.NVarChar, 50).Value = citiesdrdolstedit.SelectedValue;
cmdinst.Parameters.Add("@AdsTit", SqlDbType.NVarChar, 50).Value = addadstittxtbxedit.Text;
cmdinst.Parameters.Add("@AdsPrice", SqlDbType.Int).Value = pricetxtboxedit.Text;
cmdinst.Parameters.Add("@AdsDesc", SqlDbType.VarChar).Value = destxtboxedit.Text;
conedit.Open();
cmdinst.ExecuteNonQuery();
conedit.Close();
BIND();
}
}
protected void SubCatedrdoads_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void updateads_Click(object sender, ImageClickEventArgs e)
{
}
protected void undoupdate_Click(object sender, ImageClickEventArgs e)
{
}
protected void deleteads_Click(object sender, ImageClickEventArgs e)
{
}
}
}
 
 

Answers (2)