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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Raj KumarRank #25 · 44.2K reputationPosted Dec 16, 2010, 7:14 PM EST
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
Suthish NairRank #92 · 22.8K reputationPosted Dec 15, 2010, 6:30 AM EST
http://www.c-sharpcorner.com/UploadFile/raj1979/ListView07282008180757PM/ListView.aspx
B M SuchitraRank #2,922 · 492 reputationPosted Dec 15, 2010, 4:06 PM EST
Thanks
Subhendu DeRank #1,108 · 1.7K reputationPosted Dec 15, 2010, 7:06 PM EST
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 SuchitraRank #2,922 · 492 reputationPosted Dec 15, 2010, 9:10 PM EST
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 DeRank #1,108 · 1.7K reputationPosted Dec 15, 2010, 9:25 PM EST
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 SuchitraRank #2,922 · 492 reputationPosted Dec 15, 2010, 11:42 PM EST
Sam HobbsRank #107 · 20.5K reputationPosted Dec 16, 2010, 11:48 AM EST
There are many articles in this web site that will help you.
B M SuchitraRank #2,922 · 492 reputationPosted Dec 16, 2010, 5:09 PM EST
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