C# .NET - how to update value in database to take from listview

Asked By pravin kumar S on 15-Sep-08 09:41 AM
end of post

listview - Partha Mandayam replied to pravin kumar S on 15-Sep-08 09:56 AM

Here's an article showing how to bind a listview to a database. This will automatically update all values in the database

http://aspnet.4guysfromrolla.com/articles/010208-1.aspx

solution - Perry replied to pravin kumar S on 15-Sep-08 10:22 AM

Hi,

ListViewDataItem item2 = this.CreateDataItem(displayIndex + dataSource.StartRowIndex, displayIndex);
...
if (dataBinding)
{
item2.DataItem = obj2;
...
}
this.OnItemCreated(new ListViewItemEventArgs(item2));
...
if (dataBinding)
{
item2.DataBind();
this.OnItemDataBound(new ListViewItemEventArgs(item2));
item2.DataItem = null; // <- See this?
}

After the call to DataBind(), the item's DataItem is set to null. This means that when calling InnerView's DataBind() on your handler, the ListView's container (the ListViewDataItem object) no longer has a valid DataItem, thus the <%# $> expressions can't be evaluated and the framework's event handler will throw an exception. Since the DataSource object in the inner ListView is still valid from the previous DataBind() call (before the ItemEditing event was fired), you can use reflection to remove the framework's handler before calling DataBind() (see bellow).  So, this means you can't call DataBind() on the inner ListView while having expressions relating to the container's DataItem on your .aspx file evaluated, and you can't call DataBind() on the outer ListView since that will obviously create and render a new inner ListView.

What remains is data bind externally. You can handle the outer ListView's ItemCreated event and do something like: 

protected void ItemCreated(object sender, ListViewItemEventArgs e)
{
var lvInner = (ListView)e.Item.FindControl("InnerListView");
lvInner.DataSource = DataBinder.GetPropertyValue(((ListViewDataItem)e.Item).DataItem, "propToEval");
}
Using reflection is way cooler (and lets you keep the <%# %> tags), but may have side effects since you'll be removing all handlers associated with the DataBinding event:  

protected void ItemEditing(object sender, ListViewEditEventArgs e)
{
var innerListView = (ListView)sender;
innerListView.EditIndex = e.NewEditIndex;

var pInfo = innerListView.GetType().GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Instance);
var fInfo = typeof(Control).GetField("EventDataBinding", BindingFlags.NonPublic | BindingFlags.Static);

var handlerList = (EventHandlerList)pInfo.GetValue(innerListView, null);
var key = fInfo.GetValue(innerListView);

var handler = handlerList[key];
handlerList.RemoveHandler(key, handler);

innerListView.DataBind();
}

-Paresh

with checkboxes - Perry replied to pravin kumar S on 15-Sep-08 10:24 AM

Hi,
 
If you are planning to add checkboxes then set Auto PostBack property of your check box to true; Double cclick that checkbox.In server side code,OnCheckChanged event,write your db update code.
 
-Paresh
see this: - Binny ch replied to pravin kumar S on 15-Sep-08 12:54 PM
Use this query:
UPDATE tablename  SET FirstName = '"& textbox1.text &"'WHERE LastName = '" &Listview1.SelectedItem.text& "'
simply - Web Star replied to pravin kumar S on 16-Sep-08 01:00 AM

get the vlaue from listview

string strSelected = Listview1.SelectedItem.text;

and than u save it to database as u know

How to update value in database to take from listview - Kalit Sikka replied to pravin kumar S on 16-Sep-08 06:27 AM
Protected Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Session("Liste2") = ListBox3.SelectedItem.Value

Dim conn As New SqlConnection("Server=INSRVSN2\GESTION ;Initial Catalog=Dev_GI2;uid=dev_gi2;password=dev_gi2")

Dim cmdInsert As SqlCommand

Dim sqlInsert As String

Try

conn.Open()

For Each li As ListBox In ListBox3.Items

sqlInsert = "INSERT INTO GI_LISTEAPPLI2 (GILI_ID,GILI_APPLICATION)"

sqlInsert += "VALUES ('" + ListBox3.SelectedValue + "')"

cmdInsert.CommandText = sqlInsert

cmdInsert.ExecuteNonQuery()

Next

Catch Ex As Exception

Finally

End Try

conn.Close()

End sub

Transfering file to the web using web services in vb .net - Vinod Maurya replied to pravin kumar S on 18-Sep-08 04:38 AM
Can anyone let me know how to transfer any file using web services in vb .net. My concept is that first read the file from the disk of local computer (pc) and then send the data to the web server and then save there. Please help me. Thanks
how to change database password using asp,net with C # - Dhananjai Singh replied to pravin kumar S on 24-Apr-09 02:30 AM

Hello

I am using sql server 2005 and vs2005

and I want to a screen for updating database password to be provided, only DBA to have access to this screen

update database(sqlsever2005) user id & password using asp.net with C#
Dhananjai Singh replied to Dhananjai Singh on 24-Apr-09 02:56 AM
end of post