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:
DataSourceID="SqlDataSourceUPAddAdsCate"
oneditcommand="DataList1_EditCommand1" >
ImageUrl="~/iconsimg/update.png" />
ImageUrl="~/iconsimg/delete.png" />
style="font-weight: 700; font-size: large; color: #3366FF; font-family: 'Times New Roman', Times, serif;" Text='<%# Eval("AdsTit") %>'>
| Ads Number : | Category : | - | |
Price : | |||
| Location : | - - | ||
Keywords: |
Description :
style="text-align: justify">
ImageUrl="~/iconsimg/update.png" />
ImageUrl="~/iconsimg/undo.png" />
| Section: | onselectedindexchanged="Catedrdoads_SelectedIndexChanged"> |
| Category: | OnSelectedIndexChanged="SubCatedrdoads_SelectedIndexChanged"> |
| Country: | onselectedindexchanged="countrdrdolst_SelectedIndexChanged"> |
| State: | Height="16px" onselectedindexchanged="statedrdolst_SelectedIndexChanged"> |
| City: | onselectedindexchanged="citiesdrdolst_SelectedIndexChanged"> |
| Ads title: | |
| Ads Price: | |
| Description: | Height="85px" Width="350px" TextMode="MultiLine" Rows="12"> |
| Search Keywords: |
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)
{
}
}
}
2 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
vijayPosted May 24, 2014, 8:31 AM
Refer the below links..
http://www.dotnetcode.in/2011/07/how-to-updatedelete-and-edit-in.html
Abhijit PatilPosted May 22, 2014, 2:11 AM
can u send me zip file of your full code with what requirment your want
hope it is better for me.
thanks and Regards,
Abhijit Patil
dotnetbyabhipatil.blogspot.in