Click here to Skip to main content
15,905,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have populated listview from database.i am trying to get the datakey value of listbox bt i am getting null value:the code is:
protected void Maillist_ItemCommand(object sender, ListViewCommandEventArgs e)
   {
       if (e.CommandName == "Delete")
       {
           populatelistview();
           Label lblid=(Label)e.Item.FindControl("lblid");
           string id = Maillist.SelectedValue.ToString();
           SqlConnection conn = new SqlConnection();
           conn.ConnectionString = strconn;
           string str = "delete from Companymails_Master where Mailid='" + Maillist.DataKeys.ToString() + "'";
           SqlCommand cmd = new SqlCommand(str,conn);
           conn.Open();
           cmd.ExecuteNonQuery();
           conn.Close();

           populatelistview();
       }
   }
plz guide...where m i wrong.i simply want to delete the row on clicking the
delete button.
Posted

try this

 protected void EmployeesListView_OnItemCommand(object sender, ListViewCommandEventArgs e)
  {
    if (String.Equals(e.CommandName, "AddToList"))
    {
      // Verify that the employee ID is not already in the list. If not, add the
      // employee to the list.
      ListViewDataItem dataItem = (ListViewDataItem)e.Item;
      string employeeID = 
        EmployeesListView.DataKeys[dataItem.DisplayIndex].Value.ToString();

      if (SelectedEmployeesListBox.Items.FindByValue(employeeID) == null)
      {
        ListItem item = new ListItem(e.CommandArgument.ToString(), employeeID);
        SelectedEmployeesListBox.Items.Add(item);
      }
    }
  }


//Other way is 

<asp:LinkButton ID="LinkButton1" CommandName="Insert" CommandArgument='<%#Eval("CatId")%>'
                 runat="server">Edit</asp:LinkButton>

//in the item command Event 

   if (e.CommandName == Edit)
       {

 int CatId = int.Parse(e.CommandArgument.ToString());
}
 
Share this answer
 
v2
Have you mention DataKey attribute in the aspx ListBox Control or DataKey must be same as that of table field in the database.

Thanks & Regards,
Balwant
 
Share this answer
 
Comments
mylogics 2-May-11 8:12am    
yes i have defined datakey in listview.i also have a label name lblid which contains the primaryvalue .bt i am not able to get the value.it gives null.
Try this for your query

string str = "delete from Companymails_Master where Mailid='" + Maillist.DataKeys[e.ItemIndex].ToString() + "'";


Hope this helps .......
 
Share this answer
 
v2
Comments
mylogics 2-May-11 8:29am    
e.Item.ItemIndex there is no such property
finally got the solution here:

protected void Maillist_ItemDeleting(object sender, ListViewDeleteEventArgs e)
  {
      string id = Maillist.DataKeys[e.ItemIndex].Value.ToString();
      SqlConnection conn = new SqlConnection();
      conn.ConnectionString = strconn;
      string str = "delete from Companymails_Master where Mailid='" + id + "'";
      SqlCommand cmd = new SqlCommand(str, conn);
      conn.Open();
      cmd.ExecuteNonQuery();
      conn.Close();

      populatelistview();
  }
 
Share this answer
 
Comments
pooria2c 10-Dec-12 3:30am    
tank you for your post

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900