Hi
I am using .net 3.5 and SQL 2005... I want to save the LISTVIEW data in a table... Plzz tell me how do i do it?
Thanks
Loading
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.
Raj KumarPosted Dec 17, 2010, 12:14 AM
Dim iCount As Integer
Dim iLoop As Integer
Dim query3 As New SqlCommand
Dim lvitem
query3.Connection = New SqlConnection("Password=sasa;Persist Security Info=True;User ID=sa;Initial Catalog=Tel;Data Source=HA-PC")
query3.Connection.Open()
iCount = lvLogs.Items.Count
' For iLoop = 0 To lvLogs.Items.Count - 1
If Not lvLogs.Items.Count = 0 Then
Do Until iLoop = lvLogs.Items.Count
lvitem = lvLogs.Items.Item(iLoop)
With lvitem
query3.CommandText = "insert into A(a,b,c,d,e) values " _
& "('" & lvitem.subitems(0).text & "','" & lvitem.subitems(1).text & "','" & lvitem.subitems(2).Text & "','" & lvitem.subitems(3).Text & "','" & lvitem.subitems(4).Text & "')"
'" & .SubItems(6).Text & "','" & .SubItems(7).Text & "','" & .SubItems(8).Text & "')"
query3.ExecuteNonQuery()
End With
iLoop = iLoop + 1
lvitem = Nothing
Loop
MsgBox("done")
End If
B M SuchitraPosted Dec 16, 2010, 10:09 PM
I think u must go through the reply/solution given by suthish and then justify..I have gone through the site and did not get a solution so posted here...I did not find any post about saving listview data in a table..Listview is a shopping cart in my page whose data i need to save in a table..
Thank u
Sam HobbsPosted Dec 16, 2010, 4:48 PM
There are many articles in this web site that will help you.
B M SuchitraPosted Dec 16, 2010, 4:42 AM
Subhendu DePosted Dec 16, 2010, 2:25 AM
Description:
Now you can reference like
for (int i = 0; i < ListView1.Items.Count; i++)
{
Label lbltitle = ListView1.Items[i].FindControl("lbltitle") as Label;
Label lblDesc = ListView1.Items[i].FindControl("lblDesc") as Label;
Label lblcode = ListView1.Items[i].FindControl("lblcode") as Label;
//blah... blah... blah...
// the use lbltitle.Text, lblDesc.Text to get the value
}
If you want to reference image also, please use asp .NET imagecontrol.
B M SuchitraPosted Dec 16, 2010, 2:10 AM
In the LISTVIEW i have details of one product only...
ex:
<%#Eval("product_title")%>
<%#Eval("description")%>
code: <%#Eval("product_code")%>
<%#Eval("price")%>
time: <%#Eval("shipping_time")%>
Will your code work for this listview?
Subhendu DePosted Dec 16, 2010, 12:06 AM
Now I iterate over the listview to prepare the sql query. For your solution, you can iterate your listview and get the controls value and prepare sql string to save the data in database.
aspx code
=======
codebehind
========
public partial class WebForm4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (NorthwindDataContext _context = new NorthwindDataContext())
{
var query = from _category in _context.Categories
select _category;
List
ListView1.DataSource = _categories;
ListView1.DataBind();
}
}
}
protected void btnGetSQL_Click(object sender, EventArgs e)
{
string _sql = String.Empty;
for (int i = 0; i < ListView1.Items.Count; i++)
{
Label lblCategoryID = ListView1.Items[i].FindControl("CategoryIDLabel") as Label;
Label lblCategoryName = ListView1.Items[i].FindControl("CategoryNameLabel") as Label;
Label lblDescription = ListView1.Items[i].FindControl("DecsriptionLabel") as Label;
_sql=String.Format("INSERT INTO Categories(CategoryID, CategoryName, Description) Values({0},\"{1}\",\"{2}\")",lblCategoryID.Text,lblCategoryName.Text,lblDescription.Text);
Response.Write(_sql + "
");
}
}
}
B M SuchitraPosted Dec 15, 2010, 9:06 PM
Thanks
Suthish NairPosted Dec 15, 2010, 11:30 AM
http://www.c-sharpcorner.com/UploadFile/raj1979/ListView07282008180757PM/ListView.aspx