I have 2 gridview called DDLTOC and DDLCase. I have inserted default values into the DDL using appenddatabounditems. My default value is ("Select Member Report ID")
<asp:DropDownList ID="DDLTOC" runat="server" style="margin-top: 0px;" OnSelectedIndexChanged="DDLTOC_SelectedIndexChanged" DataTextField="typeofcrime" DataValueField="typeofcrime" AutoPostBack="True" AppendDataBoundItems="true" >
<asp:ListItem Value="-1">Select Member Report ID</asp:ListItem>
</asp:DropDownList>
DDLCase
<asp:DropDownList ID="DDLCase" runat="server" AutoPostBack="True" DataTextField="memberreportid" DataValueField="memberreportid" Height="16px" OnSelectedIndexChanged="DDLCase_SelectedIndexChanged" AppendDataBoundItems="true" >
<asp:ListItem Value="">Select Case</asp:ListItem>
</asp:DropDownList>
I have inserted data bind into my DDLTOC at the page load which will display selected value from the database once the webapp is run.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
SqlConnection conn = new SqlConnection("Data Source=localhost;" +
"Initial Catalog=project; Integrated Security = SSPI");
SqlDataAdapter da = new SqlDataAdapter("SELECT distinct typeofcrime FROM MemberReport where handle='handled' AND caseprogress='settled'", conn);
conn.Open();
DataSet ds = new DataSet();
da.Fill(ds);
DDLTOC.DataSource = ds;
DDLTOC.DataTextField = "typeofcrime";
DDLTOC.DataValueField = "typeofcrime";
DDLTOC.DataBind();
conn.Close();
}
}
I also added another bind on the DDLTOC
protected void DDLTOC_SelectedIndexChanged(object sender, EventArgs e)
{
using (var connAdd = new SqlConnection("Data Source = localhost; Initial Catalog = project; Integrated Security= SSPI"))
{
connAdd.Open();
var sql = "SELECT memberreportid FROM MemberReport Where typeofcrime ='" + DDLTOC.SelectedValue + "' AND caseprogress='settled'";
using (var cmdAdd = new SqlDataAdapter(sql, connAdd))
{
DataSet ds2 = new DataSet();
cmdAdd.Fill(ds2);
DDLCase.DataSource = ds2;
DDLCase.DataTextField = "memberreportid";
DDLCase.DataValueField = "memberreportid";
DDLCase.DataBind();
}
sql = "Select username, memberreportid, location, crdatetime, citizenreport, image1, image2, image3, image4, image5 from MemberReport where typeofcrime ='" + DDLTOC.SelectedItem.Text + "' and handle='handled'";
using (var cmdAdd = new SqlDataAdapter(sql, connAdd))
{
DataSet dsSel = new DataSet();
cmdAdd.Fill(dsSel);
GVCR.DataSource = dsSel;
GVCR.DataBind();
}
connAdd.Close();
}
}
The first binding in DDLTOC_SelectedIndex basically allows the value of DDLCase to be displayed on the dropdownlist according to the value selected in the DDLTOC. The 2nd one binding will display out the necessary values from the database into a gridview. I have 2 database value that will be displayed out in the DDLTOC, Gang & Robbery. So if i were to randomly select gang, then select back to my default value then select gang and back to selecting default value, it will display out the Gang's DDLCase value twice on my DDLCase.
Why does repetitive data occurs?