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.
Related
I have a search form that is fired from my grid view select button. It works fine when it loads as in the fields fill up with the selected row details as expected but when I change any field and click on update record it does not take the updated values that I just typed into the text box but seems to resave the original values . Please help
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Search_Reporters : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Ingest;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
//txt_rname.Enabled = false;
// txt_rmobile.Enabled = false;
// txt_remail.Enabled = false;
con.Open();
// string qry = "select * from Reporter where Reporter_ID= " + DropDownList1.SelectedValue + "";
lbl_1.Text = Session["id"].ToString();
string qry = "select * from Reporter where Reporter_ID= " + Session["id"] + " ";
SqlCommand cmd = new SqlCommand(qry, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt); // table data-> data table
con.Close();
txt_rname.Text = dt.Rows[0][1].ToString();
txt_remail.Text = dt.Rows[0][2].ToString();
txt_rmobile.Text = dt.Rows[0][3].ToString();
}
protected void btn_edit_Click(object sender, EventArgs e)
{
txt_rname.Enabled = true;
txt_rmobile.Enabled = true;
txt_remail.Enabled = true;
lbl_remark.Text = "";
}
protected void btn_Save_Click(object sender, EventArgs e)
{
con.Open();
string qry = "update Reporter set Reporter_Name ='" + txt_rname.Text + "',Reporter_Email='" + txt_remail.Text + "',Reporter_Mobile = '" + txt_rmobile.Text + "' where Reporter_ID = " +lbl_1.Text+ "";
SqlCommand cmd = new SqlCommand(qry, con);
int x = cmd.ExecuteNonQuery();
if (x == 1)
{
//lbl_remark.Text = "Updated Successfully";
Response.Redirect("Reporters_remarks.aspx");
}
else
{
lbl_remark.Text = "error";
}
con.Close();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
lbl_remark.Text = "";
txt_rname.Text = "";
txt_remail.Text = "";
txt_rmobile.Text = "";
con.Open();
string qry = "select * from Reporter where Reporter_ID= " + DropDownList1.SelectedValue + "";
//string qry = "select * from Reporter where Reporter_ID= "+Session["id"]+" ";
SqlCommand cmd = new SqlCommand(qry, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt); // table data-> data table
con.Close();
if (dt.Rows.Count == 0)
{
lbl_remark.Text = "User Not Found";
}
else
{
txt_rname.Text = dt.Rows[0][1].ToString();
txt_remail.Text = dt.Rows[0][2].ToString();
txt_rmobile.Text = dt.Rows[0][3].ToString();
}
}
}
HTML Code
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Reporters_Search.aspx.cs" Inherits="Search_Reporters" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
text-align: center;
}
.style3
{
text-align: right;
width: 648px;
}
.style4
{
text-align: center;
width: 648px;
}
.style5
{
text-decoration: underline;
font-size: larger;
}
.style6
{
text-align: center;
height: 17px;
}
.style7
{
height: 17px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td colspan="2" style="text-align: center" class="style5">
<strong>Reporter Search</strong></td>
</tr>
<tr>
<td class="style4">
</td>
<td>
</td>
</tr>
<tr>
<td class="style4" style="text-align: right">
Reporter ID:</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1" DataTextField="Reporter_ID"
DataValueField="Reporter_ID"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:IngestConnectionString %>"
SelectCommand="SELECT [Reporter_ID] FROM [Reporter] ORDER BY [Reporter_ID]">
</asp:SqlDataSource>
<asp:Button ID="btn_edit" runat="server" Text="Edit" Width="60px"
onclick="btn_edit_Click" />
</td>
</tr>
<tr>
<td class="style3">
</td>
<td>
<asp:Label ID="lbl_1" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td class="style3">
Reporter Name :</td>
<td>
<asp:TextBox ID="txt_rname" runat="server" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
</td>
<td>
</td>
</tr>
<tr>
<td class="style3">
Reporter Email :</td>
<td>
<asp:TextBox ID="txt_remail" runat="server" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
</td>
<td>
</td>
</tr>
<tr>
<td class="style3">
Reporter Mobile :</td>
<td>
<asp:TextBox ID="txt_rmobile" runat="server" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style6">
</td>
<td class="style7">
</td>
</tr>
<tr>
<td class="style6" colspan="2">
<asp:Label ID="lbl_remark" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="style6">
</td>
<td class="style7">
</td>
</tr>
<tr>
<td class="style2" colspan="2">
<asp:Button ID="btn_Save" runat="server" style="text-align: center" Text="Update Record"
Width="120px" onclick="btn_Save_Click" />
</td>
</tr>
<tr>
<td class="style4">
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Thanks Pradeep. The following code fixed my problem
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack )
{
txt_rname.Enabled = false;
txt_rmobile.Enabled = false;
txt_remail.Enabled = false;
con.Open();
// string qry = "select * from Reporter where Reporter_ID= " + DropDownList1.SelectedValue + "";
lbl_1.Text = Session["id"].ToString();
string qry = "select * from Reporter where Reporter_ID= " + Session["id"] + " ";
SqlCommand cmd = new SqlCommand(qry, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt); // table data-> data table
con.Close();
txt_rname.Text = dt.Rows[0][1].ToString();
txt_remail.Text = dt.Rows[0][2].ToString();
txt_rmobile.Text = dt.Rows[0][3].ToString();
}
else
{
}
}
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";
}
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...
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));
}