I have a Repeater which creates a table for multiple row with the same field name:
<asp:Repeater runat="server" ID="rptContent">
<HeaderTemplate>
<table border="0" style="width: 95%;">
<tr>
<td style="width: 25%;">Name</td>
<td style="width: 25%;">Last Four SSN #</td>
<td style="width: 25%;">PDF Generator</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("name").ToString() %></td>
<td><%# Eval("ssn3").ToString() %></td>
<td><asp:Button ID="btnGeneratePDF" runat="server" Text="Generate PDF For" onclick="btnGeneratePDF_Click" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
My code behind looks like this:
public void writeData()
{
Conn = new SqlConnection(cString);
Conn.Open();
nameE = txtName.Text;
var pdfPath = Path.Combine(Server.MapPath("~/PDFTemplates/9.pdf"));
// Get the form fields for this PDF and fill them in!
var formFieldMap = PDFHelper.GetFormFieldNames(pdfPath);
formFieldMap["topmostSubform[0].Page1[0].f1_01_0_[0]"] = txtName.Text;
sqlCode = "SELECT * FROM [db].[dbo].[TablePDFTest] WHERE [name] = '" + nameE + "'";
using (SqlCommand command = new SqlCommand(sqlCode, Conn))
{
command.CommandType = CommandType.Text;
using (reader = command.ExecuteReader())
{
if (reader.HasRows)
{
if (reader.Read())
{
formFieldMap["topmostSubform[0].Page1[0].f1_02_0_[0]"] = reader.GetValue(1).ToString();
formFieldMap["topmostSubform[0].Page1[0].f1_04_0_[0]"] = reader.GetValue(2).ToString();
formFieldMap["topmostSubform[0].Page1[0].f1_05_0_[0]"] = reader.GetValue(3).ToString();
formFieldMap["topmostSubform[0].Page1[0].f1_07_0_[0]"] = reader.GetValue(4).ToString();
formFieldMap["topmostSubform[0].Page1[0].social[0].TextField1[0]"] = reader.GetValue(5).ToString();
formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[0]"] = reader.GetValue(6).ToString();
formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[1]"] = reader.GetValue(7).ToString();
formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[2]"] = reader.GetValue(8).ToString();
formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[3]"] = reader.GetValue(9).ToString();
}
}
}
}
// Requester's name and address (hard-coded)
formFieldMap["topmostSubform[0].Page1[0].f1_06_0_[0]"] = "Medical Group\n2700 Wr Ave\nPurchase, NY 10232";
var pdfContents = PDFHelper.GeneratePDF(pdfPath, formFieldMap);
PDFHelper.ReturnPDF(pdfContents, "Compl.pdf");
}
How can I make it so if there are more than one entries each button will query by [name] where the last four ssn is different?
First, just a nod to using parameters...
Anyway, a lot of ways to do this, but one could be to add a commandargument value to your button (and then evaluate it in your code behind).
<asp:Button
ID="btnGeneratePDF"
runat="server"
Text="Generate PDF For"
CommandArgument = '<%# Eval("L4_SSN") %>'
onclick="btnGeneratePDF_Click" />
Then you'd need to make sure you had a field in your SELECT statement such as RIGHT(SSN,4) as L4_SSN
And finally, you'd modify your btnGeneratePDF_Click sub to evaluate e.CommandArgument...
Related
I have a listview that tells the quantity ordered and quantity that has been delivered. I have complete added the first row, but the succeeding rows are not added. I want to add multiple records in one click. I have an add button outside the list view to do the job
Here is the list view of the form
<asp:ListView ID="lvPODetails" runat="server">
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblProduct" runat="server" type="number" Text='<%# Eval("RefNo")%>' class="form-control" Visible ="false" />
<%# Eval("ProductName") %>
</td>
<td>
<%# Eval("Price", "{0: #,###.00}") %>
</td>
<td><asp:TextBox ID="txtOrdered" runat="server" type="number" Text='<%# Eval("DesiredQuantity") %>' readonly="true" class="form-control" /></td>
<td><asp:TextBox ID="txtQuantity" runat="server" type="number" min="1" max="1000000" Text='' class="form-control" required /></td>
<td><%# Eval("POAmount", "{0: #,###.00}") %></td>
<td><%# Eval("POAmount", "{0: #,###.00}") %></td>
</tr>
</ItemTemplate>
<EmptyDataTemplate>
<tr>
<td colspan="4"><h2 class="text-center">No records found.</h2></td>
</tr>
</EmptyDataTemplate>
</asp:ListView>
The .cs of the aspx
protected void btnAdd_Click(object sender, EventArgs e)
{
bool existingSupply = IsExisting();
foreach (ListViewItem item in lvPODetails.Items)
{
TextBox quantity = (TextBox)item.FindControl("txtQuantity");
Label ltr = (Label)item.FindControl("lblProduct");
TextBox odr = (TextBox)item.FindControl("txtOrdered");
string name = ltr.Text;
int delivered = Convert.ToInt32(quantity.Text);
int ordered = Convert.ToInt32(odr.Text);
string status = String.Empty;
if (delivered >= ordered)
{
status = "Complete";
}
if (delivered < ordered)
{
status = "Partially Completed";
}
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
if (existingSupply)
{
cmd.CommandText = "UPDATE Inventory SET Quantity=quantity + #Quantity WHERE ProductID=#ProductID";
}
else
{
cmd.CommandText = "INSERT INTO Inventory VALUES (#ProductID, #ProdCatID, #SupplierName, " +
"#Quantity, #CriticalLevel, #Price, #Status, #DateAdded, #DateModified)";
}
cmd.Parameters.AddWithValue("#ProductID", name);
cmd.Parameters.AddWithValue("#ProdCatID", DBNull.Value);
cmd.Parameters.AddWithValue("#SupplierName", txtSupplier.Text);
cmd.Parameters.AddWithValue("#Quantity", delivered);
cmd.Parameters.AddWithValue("#CriticalLevel", DBNull.Value);
cmd.Parameters.AddWithValue("#Price", DBNull.Value);
cmd.Parameters.AddWithValue("#Status", DBNull.Value);
cmd.Parameters.AddWithValue("#DateAdded", DateTime.Now);
cmd.Parameters.AddWithValue("#DateModified", DateTime.Now);
//cmd.CommandText = "UPDATE PurchaseOrder SET POStatus=#POStatus WHERE PONo=#PONo";
//cmd.Parameters.AddWithValue("#POStatus", status);
//cmd.Parameters.AddWithValue("#PONo", txtPONo.Text);
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("Default.aspx");
}
}
I don't get it, it is not showing errors but it is not adding the next rows.
I am using repeater control and showing paging '<< Previous next >>' it is working fine.
But i want to show links (rolling numbers) in between like below:
Previous << 1 2 3 4 5 next >>
have a look this picture for idea.
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table border="1" style="border-color: #336699;" cellspacing="0">
<tr style="background-color:#336699; color:White;">
<td>Product ID</td>
<td>Product Name</td>
<td>Unit Price</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("ProductID") %></td>
<td><%# Eval("ProductName") %></td>
<td><%# Eval("UnitPrice") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<div> <table width="500px" border="0" cellspacing="5" cellpadding="5">
<tr>
<td width="25%"> <asp:Label ID="Label2" runat="server" />
</td>
<td width="25%"><asp:HyperLink ID="linkPrev" runat="server">« Previous</asp:HyperLink>
</td>
<td width="25%"><asp:HyperLink ID="linkNext" runat="server">Next »</asp:HyperLink></td>
<td width="25%"><asp:Label ID="Label1" runat="server" /> </td>
</tr>
</table> </div>
c#
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from Products", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
Repeater1.DataSource = ds;
Repeater1.DataBind();
con.Close();
DataTable table = new DataTable();
da.Fill(table);
PagedDataSource pds = new PagedDataSource();
pds.DataSource = table.DefaultView;
pds.AllowPaging = true;
pds.PageSize = 10;
int currentPage;
if (Request.QueryString["page"] != null)
{
currentPage = Int32.Parse(Request.QueryString["page"]);
}
else
{
currentPage = 1;
}
pds.CurrentPageIndex = currentPage - 1;
Label1.Text = "Page " + currentPage + " of " + pds.PageCount;
Label2.Text = "Results" + pds.DataSourceCount;
if (!pds.IsFirstPage)
{
linkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (currentPage - 1);
}
if (pds.IsFirstPage)
{
linkPrev.Visible = false;
linkPrev.Style["display"] = "block";
}
if (!pds.IsLastPage)
{
linkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (currentPage + 1);
}
if (pds.IsLastPage)
{
linkNext.Visible = false;
linkPrev.Style["display"] = "block";
}
Repeater1.DataSource = pds;
Repeater1.DataBind();
}
}
I am getting an error("input string was not in a correct format") every time I click the lnkUpdate button. Am i missing something? I can't find what's wrong with the codes in the updating event
aspx
<table class="table table-bordered table-hover table-responsive">
<thead>
<tr>
<th>Product ID</th>
<th>Product Name</th>
<th>Received</th>
<th>Remaining</th>
<th>Ordered</th>
<th></th>
</tr>
</thead>
<tbody>
<asp:ListView ID="lvSODetails" runat="server"
onitemcanceling="lvSODetails_ItemCanceling"
onitemediting="lvSODetails_ItemEditing"
onitemupdating="lvSODetails_ItemUpdating" >
<ItemTemplate>
<tr>
<td><%# Eval("ProductID") %></td>
<td><%# Eval("ProductName") %></td>
<td><%# Eval("Received") %></td>
<td><%# Eval("Remaining")%></td>
<td><%# Eval("Quantity") %></td>
<td>
<asp:LinkButton ID="lnkEdit" runat="server"
class="glyphicon glyphicon-pencil" CommandName="Edit" />
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<asp:Panel ID="pnlDetails" runat="server" DefaultButton="lnkUpdate">
<tr>
<td><asp:Label ID="lblID" runat="server" Text='<%# Eval("ProductID") %>' /></td>
<td><%# Eval("ProductName") %></td>
<td><asp:TextBox ID="txtQtyReceived" runat="server" /></td>
<td><asp:Label ID="lblRemaining" runat="server" Text='<%# Eval("Remaining") %>' /></td>
<td><%# Eval("Quantity") %></td>
<td>
<asp:LinkButton ID="lnkUpdate" runat="server"
class="glyphicon glyphicon-ok" CommandName="Update" />
<asp:LinkButton ID="lnkCancel" runat="server"
class="glyphicon glyphicon-remove" CommandName="Cancel" />
</td>
</tr>
</asp:Panel>
</EditItemTemplate>
</asp:ListView>
</tbody>
</table>
c# code
protected void lvSODetails_ItemEditing(object sender, ListViewEditEventArgs e)
{
lvSODetails.EditIndex = e.NewEditIndex;
GetInfo();
GetDetails();
}
protected void lvSODetails_ItemCanceling(object sender, ListViewCancelEventArgs e)
{
lvSODetails.EditIndex = -1;
GetInfo();
GetDetails();
}
protected void lvSODetails_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
string ProdID = (lvSODetails.Items[e.ItemIndex].FindControl("lblID") as Label).Text;
string Received = (lvSODetails.Items[e.ItemIndex].FindControl("txtQtyReceived") as TextBox).Text;
string Remaining = (lvSODetails.Items[e.ItemIndex].FindControl("lblRemaining") as Label).Text;
if (int.Parse(Received) < 0) // <~ this is where the code stops (error"input string was not in a correct format")
{
error.Visible = true;
lblError.Text = "Items received must not be lower than zero";
}
else if (int.Parse(Received) >= 0 && int.Parse(Received) <= int.Parse(Remaining))
{
SODetails = (DataTable)Session["sodelivery"];
foreach (DataRow row in SODetails.Rows)
{
if (row["ProductID"].ToString() == ProdID)
{
row["Received"] = Received;
break;
}
}
}
lvSODetails.EditIndex = -1;
GetInfo();
GetDetails();
}
~~~~ Edit ~~~~
listview databinding code
void GetDetails()
{
if (SODetails == null)
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT sod.ProductID, p.ProductName, sod.SOQtyReceived AS Received, " +
"(sod.SOQtyOrdered - sod.SOQtyReceived) AS Remaining, sod.SOQtyOrdered AS Quantity FROM SODetails AS sod " +
"INNER JOIN Products AS p ON sod.ProductID=p.ProductID WHERE sod.SONo=#SONo";
cmd.Parameters.Add("#SONo", SqlDbType.Int).Value = Request.QueryString["ID"].ToString();
SqlDataAdapter data = new SqlDataAdapter(cmd);
SODetails = new DataTable();
data.Fill(SODetails);
DataRow[] rowList = SODetails.Select();
foreach (DataRow dr in rowList)
{
dr["Received"] = "0";
}
lvSODetails.DataSource = SODetails;
lvSODetails.DataBind();
con.Close();
Session["sodelivery"] = SODetails;
}
else
{
lvSODetails.DataSource = SODetails;
lvSODetails.DataBind();
Session["sodelivery"] = SODetails;
}
}
Your problem isn't that you can't access the TextBox.
Use int.TryParse instead of int.Parse since you are reading user input.
int received;
if (!int.TryParse(Received, out received) || received < 0)
{
error.Visible = true;
lblError.Text = "Items received must not be lower than zero";
}
This is my aspx code...
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" RepeatDirection="Horizontal"
Width="88%" Height="100px">
<ItemTemplate>
<table width="0" border="0" cellpadding="0" cellspacing="1">
<tr>
<td>
<%#Eval("Coupon_Image")%>'
</td>
</tr>
<tr>
<td height="20px">
</td>
</tr>
<tr>
<td align="center">
<%--<asp:LinkButton ID="Button1" runat="server" Width="145" Height="34" border="0" CommandArgument='<%# Eval("Coupon_Id") %>'
CommandName="Get Coupoun" OnClick="getCoupon_Click"></asp:LinkButton>--%>
<asp:Button ID="getCoupon" runat="server" Width="145" Height="34" border="0" CommandArgument='<%# Eval("Coupon_Id")%>' Text="Get Coupon"
OnClick="getCoupon_Click" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
my code in code behind:
private void Binddata()
{
DataTable dtReport = new DataTable();
DataColumn Coupon_Id = new DataColumn("Coupon_Id");
DataColumn Coupon_Image = new DataColumn("Coupon_Image");
dtReport.Columns.Add(Coupon_Id);
dtReport.Columns.Add(Coupon_Image);
string sendquery = "select Coupon_Id,Coupon_Image from Admin where Coupon_Status='Active'";
SqlDataAdapter da = new SqlDataAdapter(sendquery, connection);
DataSet dsProfile = new DataSet();
da.Fill(dsProfile);
for (int i = 0; i < dsProfile.Tables[0].Rows.Count; i++)
{
DataRow row = dtReport.NewRow();
row["Coupon_Id"] = dsProfile.Tables[0].Rows[i]["Coupon_Id"].ToString();
string id = dsProfile.Tables[0].Rows[i]["Coupon_Id"].ToString();
string ImgName = dsProfile.Tables[0].Rows[i]["Coupon_Image"].ToString();
string Path = "http://localhost:2872/arpita-coupons/Coupon_Image/" + ImgName ;
row["Coupon_Image"] = "<img src='" + Path + "' height='200px' width='200px' />";
hddnpath.Value = Path + ImgName + id;
dtReport.Rows.Add(row);
}
DataList1.DataSource = dtReport;
DataList1.DataBind();
}
protected void getCoupon_Click(object sender, EventArgs e)
{
string id = hddnpath.Value;
string imagename = hddnpath.Value;
Response.Redirect("deals-forms.aspx?id="+id+" & ImgName="+imagename+" "+"");
}
my question is if i click the button to get the particular image to be redirect to the another page . but i am getting only the different image to the another aspx page...
You set id and imagename to the same value, then build a link from both. I guess there is the problem somewhere:
string id = hddnpath.Value;
string imagename = hddnpath.Value;
Response.Redirect("deals-forms.aspx?id=" + id // <-- this variable
+ " & ImgName=" + imagename // <-- and this one both
+ " " + ""); // contain hddnpath.Value
I want to assign same datasource to multiple DropDownLists in a form, when the page loads, only the first drop down list has filled the content of datasource, others are empty, what's the missing point? thanks for answers. Here are the codes;
<form id="form1" runat="server">
<asp:ScriptManager ID="scriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="panel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div id="div1" align="center">
<table>
<tr>
<td><b>Brand-Model</b></td>
<td><asp:TextBox ID="brandText" runat="server" BorderStyle="Inset"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="brandText" Display="Dynamic" ErrorMessage="*"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td><b>Black</b></td>
<td><asp:DropDownList ID="blackList" runat="server"></asp:DropDownList></td>
<td><asp:HyperLink ID="HyperLink1" runat="server" Text="Add Cartridge" NavigateUrl="~/Admin/addCartridge.aspx"></asp:HyperLink></td>
</tr>
<tr>
<td><b>Color1</b></td>
<td><asp:DropDownList ID="colorList1" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td><b>Color2</b></td>
<td><asp:DropDownList ID="colorList2" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td><b>Color3</b></td>
<td><asp:DropDownList ID="colorList3" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td><b>Other1</b></td>
<td><asp:DropDownList ID="otherColor1" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td><b>Other2</b></td>
<td><asp:DropDownList ID="otherColor2" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td><b>Other3</b></td>
<td><asp:DropDownList ID="otherColor3" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="submit" runat="server" Text="Submit" OnClick="submit_OnClick" /></td>
</tr>
<tr>
<td></td>
<td><asp:Label ID="submitInfo" runat="server"></asp:Label></td>
</tr>
</table>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="submit" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</form>
protected void FillTheDropDownLists()
{
SqlApplication con = new SqlApplication();
try
{
SqlCommand cmd = new SqlCommand("SELECT name FROM BT.dbo.Cartridge ORDER BY name", con.GetConnection());
con.OpenSqlConnection();
SqlDataReader reader = cmd.ExecuteReader();
blackList.DataValueField = "name";
blackList.DataSource = reader;
blackList.DataBind();
colorList1.DataValueField = "name";
colorList1.DataSource = reader;
colorList1.DataBind();
colorList2.DataValueField = "name";
colorList2.DataSource = reader;
colorList2.DataBind();
colorList3.DataValueField = "name";
colorList3.DataSource = reader;
colorList3.DataBind();
otherColor1.DataValueField = "name";
otherColor1.DataSource = reader;
otherColor1.DataBind();
otherColor2.DataValueField = "name";
otherColor2.DataSource = reader;
otherColor2.DataBind();
otherColor3.DataValueField = "name";
otherColor3.DataSource = reader;
otherColor3.DataBind();
reader.Close();
}
catch (Exception err)
{
System.Diagnostics.Debug.WriteLine("Exception: " + err.Message);
}
finally
{
con.CloseSqlConnection();
}
}
Its becasue you are using dataReader instead of this use datatable will work for you. Reader is readonly and forward only thats why only first dropdonw get filled with data and others are empty.
changed code :
protected void FillTheDropDownLists()
{
SqlApplication con = new SqlApplication();
try
{
SqlCommand cmd = new SqlCommand("SELECT name FROM BT.dbo.Cartridge ORDER BY name", con.GetConnection());
con.OpenSqlConnection();
SqlDataReader reader = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader );
blackList.DataValueField = "name";
blackList.DataSource = dt ;
blackList.DataBind();
colorList1.DataValueField = "name";
colorList1.DataSource = dt ;
colorList1.DataBind();
colorList2.DataValueField = "name";
colorList2.DataSource = reader;
colorList2.DataBind();
colorList3.DataValueField = "name";
colorList3.DataSource = dt ;
colorList3.DataBind();
otherColor1.DataValueField = "name";
otherColor1.DataSource = dt ;
otherColor1.DataBind();
otherColor2.DataValueField = "name";
otherColor2.DataSource = dt ;
otherColor2.DataBind();
otherColor3.DataValueField = "name";
otherColor3.DataSource = dt ;
otherColor3.DataBind();
reader.Close();
}
catch (Exception err)
{
System.Diagnostics.Debug.WriteLine("Exception: " + err.Message);
}
finally
{
con.CloseSqlConnection();
}
}
According to the documentation data reader is forward only. Use data table or read into a list and bind your combos to it.
for (int i = 1; i <= 260; i++)
{
ContentPlaceHolder maincontent = Page.Master.FindControl("MainContent") as ContentPlaceHolder;
DropDownList a = (DropDownList)maincontent.FindControl("ddl" + i);
a.DataSource = ds;
a.DataTextField = "Options";
a.DataValueField = "id";
a.DataBind();
a.Items.Insert(0, new ListItem("Select", "0", true));
}