4
Answers

how do I Insert selected value of listview to database?

mahnaz af

mahnaz af

9y
506
1
Hi everyone,
i want to insert selected value of listview to database but this code does not work.please help me.
 
<asp:ListView ID="ListView1" runat="server" DataKeyNames="pid" DataSourceID="SqlDataSource1"
GroupItemCount="3">
<GroupTemplate>
<tr id="itemPlaceholderContainer" runat="server">
<td id="itemPlaceholder" runat="server">
</td>
</tr>
</GroupTemplate>
<LayoutTemplate>
<table cellpadding="2" runat="server" border="1" style="border-collapse: collapse;
border-color: #999999; border-style: none; border-width: 1px;" id="tblProduct">
<tr runat="server" id="groupPlaceholder">
</tr>
</table>
<table align="center">
<tr id="r1" runat="server">
<td id="Td2" runat="server" style="text-align: center; background-color: #ffffff;
font-family: Verdana, Arial, Helvetica, sans-serif; color: #BA2323">
<asp:DataPager runat="server" ID="DataPager" PageSize="9">
<Fields>
<asp:NumericPagerField ButtonCount="6" PreviousPageText="<--" NextPageText="-->" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<td id="Td1" valign="top" align="center" style="width: 100" runat="server">
<br />
<asp:Label ID="lblpid" runat="server" Text='<%# Eval("pid") %>' />
<asp:Label ID="titleLabel" runat="server" Text='<%# Eval("title") %>' />
<br />
<asp:Image ID="Image1" runat="server" src='<%# Eval("tozihat") %>' />
<br />
<asp:Label ID="lblprice" runat="server" Text='<%# Eval("price") %>'></asp:Label>
<br />
<br />
<asp:PlaceHolder ID="AvailPlaceHolder" runat="server" Visible='<%# (Eval("mojoud")=="a") %>'>
<tr>
<td>
<asp:Label ID="lblAvail" runat="server" Text="????? ???"></asp:Label>
</td>
<td colspan="27">
</td>
</tr>
</asp:PlaceHolder>
<br />
<asp:TextBox ID="txtcount" type="text" runat="server" Width="30"></asp:TextBox>
<asp:Button ID="addtobasket" runat="server" Text="Add to basket" OnClick="addtobasket_Click">
</asp:Button>
<br />
<asp:LinkButton ID="lnkSelect" Text="Select" CommandName="Select" runat="server" />
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%#"Continue.aspx?pid=" + Eval("pid") %>'>...??????? ????? </asp:HyperLink>
</td>
</ItemTemplate>
<SelectedItemTemplate>
<td id="Td3" runat="server">
<asp:LinkButton ID="lnkSelect" Text="Select" CommandName="Select" runat="server" />
</td>
</SelectedItemTemplate>
</asp:ListView>
 
 
 
 
protected void ListView1_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
{
ListViewItem item = (ListViewItem)ListView1.Items[e.NewSelectedIndex];
if (Session["username"] == null)
{
Response.Write("???? ???? ????? ???? ???? ?????? ??? ????");
}
else
{
DataSet1 ds = new DataSet1();
DataSet1TableAdapters.tblFactorTableAdapter ta = new DataSet1TableAdapters.tblFactorTableAdapter();
DataSet1TableAdapters.tblOrderTableAdapter t6 = new DataSet1TableAdapters.tblOrderTableAdapter();
DataSet1TableAdapters.tblOrderTableAdapter ta7 = new DataSet1TableAdapters.tblOrderTableAdapter();
ta.FillByUser(ds.tblFactor, Session["username"].ToString(), false);
Label pidlabel = (Label)item.FindControl("lblpid");
int pid = Convert.ToInt32(pidlabel);
Label pricelabel = (Label)item.FindControl("lblprice");
decimal price = Convert.ToDecimal(pricelabel);
TextBox textbx = (TextBox)item.FindControl("txtcount");
int count = Convert.ToInt32(textbx);
// Response.Write("tedad=" + tedad);
decimal finalprice = price * count;
if (ds.tblFactor.Rows.Count > 0)
{
int fid = int.Parse(ds.tblFactor.Rows[0]["fid"].ToString());
ta7.InsertQuery(fid, pid, price, finalprice, count);
}
else
{
PersianCalendar pc = new PersianCalendar();
string tarikh = pc.GetYear(DateTime.Now) + "/" + pc.GetMonth(DateTime.Now) + "/" + pc.GetDayOfMonth(DateTime.Now);
string saat = DateTime.Now.Hour + ":" + DateTime.Now.Minute;
ta.Insert(tarikh, saat, Session["username"].ToString(), false);
ta.FillByUser(ds.tblFactor, Session["username"].ToString(), false);
int fid = int.Parse(ds.tblFactor.Rows[0]["fid"].ToString());
ta7.InsertQuery(fid, pid, price, finalprice, count);
}
}
}
 
Answers (4)