ddl is reverting on postbacks - asp.net

so I've been developing an asp.net web site and it has cascading dropdown. I didn't use ajax extender for cascading dropdown. here is the html mark up of my project
SPOILER, it has lot and lot of checkboxes, that's why i really need to have a workaround for checkboxes or postbacks..
<form id="form1" runat="server">
<div class = "header" runat = "server">
<div class = "osv"></div>
</div>
<div class = "center">
<div class = "box">
<table id = "spot">
<tr>
<td><asp:Label ID="LBLPlatform" runat="server" Text="Platform :"></asp:Label></td>
<td><asp:DropDownList ID="DDLPlatform" runat="server" CssClass = "ddl" AutoPostBack="True" >
<asp:ListItem Text="Platform" Value="Platform" ></asp:ListItem>
<asp:ListItem Text="First Person" Value="F1"></asp:ListItem>
<asp:ListItem Text="3rd Person" Value="F3"></asp:ListItem>
<asp:ListItem Text="Role Playing" Value="RP"></asp:ListItem>
<asp:ListItem Text="Multiplayer" Value="MP"></asp:ListItem>
<asp:ListItem Text="Single Player" Value="SP"></asp:ListItem>
</asp:DropDownList>
</td>
<td><asp:Label ID="LBLDate" runat="server" Text="Date :"></asp:Label></td>
<td><asp:TextBox ID="DDLDate" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Label ID="LBLAgency" runat="server" Text="Agency :"></asp:Label></td>
<td><asp:DropDownList ID="DDLAgency" runat="server" AutoPostBack="true" CssClass = "ddl" >
<asp:ListItem Text="Agency"></asp:ListItem>
</asp:DropDownList></td>
<td><asp:Label ID="LBLReference" runat="server" Text="Reference :"></asp:Label></td>
<td><asp:TextBox ID = "TXTReference" runat = "server"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Label ID="LBLAdvertiser" runat="server" Text="Advertiser :"></asp:Label></td>
<td><asp:DropDownList ID="DDLAdvert" runat="server" AutoPostBack="true" CssClass = "ddl" >
<asp:ListItem Text="Advertiser" ></asp:ListItem>
</asp:DropDownList></td>
<td><asp:Label ID = "LBLProgram" runat = "server" Text = "Program"></asp:Label></td>
<td><asp:DropDownList ID = "DDLProg" runat = "server" CssClass = "ddl"></asp:DropDownList></td>
</tr>
<tr>
<td><asp:Label ID = "LBLProduct" runat = "server" Text = "Product :"></asp:Label></td>
<td><asp:DropDownList ID="DDLProd" runat="server" AutoPostBack="true" CssClass = "ddl" >
<asp:ListItem Text="Product"></asp:ListItem>
</asp:DropDownList></td>
<td><asp:Label ID = "LBLTeldate" runat = "server" Text = "Telecast Date :"></asp:Label></td>
<td><asp:TextBox ID="datepicker" runat="server" onclick="return datepicker_onclick()" AutoPostBack="true" ></asp:TextBox></td>
</tr>
<tr>
<td><asp:Label ID="LBLVerleng" runat="server" Text="Version/Length :"></asp:Label></td>
<td><asp:DropDownList ID="DDLVer" runat="server" CssClass = "ddl" EnableViewState="true" AppendDataBoundItems="True"> </asp:DropDownList>
</td>
<td><asp:Label ID="LBLAe" runat="server" Text="Account Executive :"></asp:Label></td>
<td><asp:TextBox ID="TXTAe" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Label ID="LBLCost" runat="server" Text="Cost :"></asp:Label></td>
<td><asp:TextBox ID="TXTCost" runat="server"></asp:TextBox></td>
</tr>
</table>
this is my code behind where it loads the cascading ddls
Protected Sub dbchose() Handles DDLPlatform.SelectedIndexChanged
If IsPostBack Then
Select Case DDLPlatform.SelectedValue
Case "Platform"
DDLAgency.DataSource = New List(Of String)
DDLAgency.DataBind()
DDLAgency.Items.Add("Agency")
DDLAdvert.DataSource = New List(Of String)
DDLAdvert.DataBind()
DDLAdvert.Items.Add("Advertiser")
DDLProd.DataSource = New List(Of String)
DDLProd.DataBind()
DDLProd.Items.Add("Product")
DDLVer.DataSource = New List(Of String)
DDLVer.DataBind()
DDLVer.Items.Add("Version - Length")
DDLProg.DataSource = New List(Of String)
DDLProg.DataBind()
DDLProg.Items.Add("Program")
Case "F1"
Session("platform") = "F1"
Dim conn As New SqlConnection(Session("cs").ToString)
Dim que As String = "Select ag_agencde, ag_agennme from agency where ag_stat= 'A' order by ag_agennme asc"
Dim cmd As New SqlCommand(que, conn)
conn.Open() 'loading of agency per platform
DDLAgency.DataSource = cmd.ExecuteReader
DDLAgency.DataTextField = "ag_agennme"
DDLAgency.DataValueField = "ag_agencde"
DDLAgency.DataBind()
conn.Close()
'-- Loading List of Program per Platform - BMS
Dim que2 As String = "select pg_prgcode, pg_prgname from program where pg_status='A' order by pg_prgname ASC"
Dim cmd2 As New SqlCommand(que2, conn)
conn.Open()
DDLProg.DataSource = cmd2.ExecuteReader
DDLProg.DataTextField = "pg_prgname"
DDLProg.DataValueField = "pg_prgcode"
DDLProg.DataBind()
conn.Close()
'--
Case "F3"
Session("platform") = "F3"
Dim conn As New SqlConnection(Session("cs").ToString)
Dim que As String = "Select ag_agencde, ag_agennme from agency where ag_stat='A' order by ag_agennme asc"
Dim cmd As New SqlCommand(que, conn)
conn.Open() 'loading of agency per platform
DDLAgency.DataSource = cmd.ExecuteReader
DDLAgency.DataTextField = "ag_agennme"
DDLAgency.DataValueField = "ag_agencde"
DDLAgency.DataBind()
conn.Close()
'--loading of F3 programs
Dim que2 As String = "select pg_prgcode, pg_prgname from program where pg_status = 'A' order by pg_prgname ASC "
Dim cmd2 As New SqlCommand(que2, conn)
conn.Open()
DDLProg.DataSource = cmd2.ExecuteReader
DDLProg.DataTextField = "pg_prgname"
DDLProg.DataValueField = "pg_prgcode"
DDLProg.DataBind()
'--
Case "RP"
Session("platform") = "RP"
Dim conn As New SqlConnection(Session("cs").ToString)
Dim que As String = "Select ag_agencde, ag_agennme from agency where ag_stat='A' order by ag_agennme asc"
Dim cmd As New SqlCommand(que, conn)
conn.Open()
'--
DDLAgency.DataSource = cmd.ExecuteReader
DDLAgency.DataTextField = "ag_agennme"
DDLAgency.DataValueField = "ag_agencde"
DDLAgency.DataBind()
conn.Close()
'--loading of program per plaform - News TV
Dim que2 As String = "select pg_prgcode, pg_prgname from program where pg_status='A' order by pg_prgname ASC"
Dim cmd2 As New SqlCommand(que2, conn)
conn.Open()
DDLProg.DataSource = cmd2.ExecuteReader
DDLProg.DataTextField = "pg_prgname"
DDLProg.DataValueField = "pg_prgcode"
DDLProg.DataBind()
conn.Close()
'--
Case "MP"
Session("platform") = "Radio"
Dim conn As New SqlConnection(Session("cs").ToString)
Dim que As String = "Select ag_agencde, ag_agennme from agency where ag_stat='A' order by ag_agennme asc"
Dim cmd As New SqlCommand(que, conn)
conn.Open()
'--
DDLAgency.DataSource = cmd.ExecuteReader
DDLAgency.DataTextField = "ag_agennme"
DDLAgency.DataValueField = "ag_agencde"
DDLAgency.DataBind()
conn.Close()
'--loading of program for radio
Dim que2 As String = "select pg_prgcode, pg_prgname from program where pg_stat='A' order by pg_prgname ASC"
Dim cmd2 As New SqlCommand(que2, conn)
conn.Open()
DDLProg.DataSource = cmd2.ExecuteReader
DDLProg.DataTextField = "pg_prgname"
DDLProg.DataValueField = "pg_prgcode"
DDLProg.DataBind()
conn.Close()
'--
Case "SP"
Session("platform") = "SP"
Dim conn As New SqlConnection(Session("cs").ToString)
Dim que As String = "Select ag_agencde, ag_agennme from agency where ag_stat='A' order by ag_agennme asc"
Dim cmd As New SqlCommand(que, conn)
conn.Open()
'--
DDLAgency.DataSource = cmd.ExecuteReader
DDLAgency.DataTextField = "ag_agennme"
DDLAgency.DataValueField = "ag_agencde"
DDLAgency.DataBind()
conn.Close()
'--loading of programs Pinoy TV
Dim que1 As String = "select pg_prgcode, pg_prgname from program where pg_status='A' order by pg_prgname ASC"
Dim cm2 As New SqlCommand(que1, conn)
conn.Open()
DDLProg.DataSource = cm2.ExecuteReader
DDLProg.DataTextField = "pg_prgname"
DDLProg.DataValueField = "pg_prgcode"
DDLProg.DataBind()
End Select
End If
End Sub
Protected Sub agen_choose() Handles DDLAgency.SelectedIndexChanged
If IsPostBack = True Then
Select Case Session("platform").ToString
Case "F1"
Dim que As String = "select ad_advcde, ad_advnme from advertiser where ad_stat='A' order by ad_advnme ASC"
Dim cmd2 As New SqlCommand(que, conn)
conn.Open()
DDLAdvert.DataSource = cmd2.ExecuteReader
DDLAdvert.DataTextField = "ad_advnme"
DDLAdvert.DataValueField = "ad_advcde"
DDLAdvert.DataBind()
conn.Close()
'--
''--
Case "F3"
Dim cs As String = "server=SQLREP;database=F3;uid=sa;password=sa;"
conn.ConnectionString = cs
Dim que As String = "select ad_advcde, ad_advnme from advertiser where ad_stat='A' order by ad_advnme ASC"
Dim cmd2 As New SqlCommand(que, conn)
conn.Open()
DDLAdvert.DataSource = cmd2.ExecuteReader
DDLAdvert.DataTextField = "ad_advnme"
DDLAdvert.DataValueField = "ad_advcde"
DDLAdvert.DataBind()
conn.Close()
'--
Case "RP"
Dim que As String = "select ad_advcde, ad_advnme from RPSA.advertiser where ad_stat='A' order by ad_advnme ASC"
Dim cmd2 As New SqlCommand(que, conn)
conn.Open()
DDLAdvert.DataSource = cmd2.ExecuteReader
DDLAdvert.DataTextField = "ad_advnme"
DDLAdvert.DataValueField = "ad_advcde"
DDLAdvert.DataBind()
conn.Close()
'--
Case "Radio"
Dim que As String = "select ad_advcde, ad_advnme from advertiser where ad_stat='A' order by ad_advnme ASC"
Dim cmd2 As New SqlCommand(que, conn)
conn.Open()
DDLAdvert.DataSource = cmd2.ExecuteReader
DDLAdvert.DataTextField = "ad_advnme"
DDLAdvert.DataValueField = "ad_advcde"
DDLAdvert.DataBind()
conn.Close()
Case "SP"
Dim que As String = "select ad_advcde, ad_advnme from SPSA.advertiser where ad_stat='A' order by ad_advnme ASC"
Dim cmd2 As New SqlCommand(que, conn)
conn.Open()
DDLAdvert.DataSource = cmd2.ExecuteReader
DDLAdvert.DataTextField = "ad_advnme"
DDLAdvert.DataValueField = "ad_advcde"
DDLAdvert.DataBind()
conn.Close()
End Select
End If
End Sub
Protected Sub adver_choose() Handles DDLAdvert.SelectedIndexChanged
Dim conn As New SqlConnection(Session("cs").ToString)
If IsPostBack = True Then
Select Case Session("platform").ToString
Case "F1"
Dim que3 As String = "select p_pcode, p_pname from product where p_stat = 'A' and p_advcde = " & DDLAdvert.SelectedValue & " order by p_pname asc"
Dim cmd3 As New SqlCommand(que3, conn)
conn.Open()
DDLProd.DataSource = cmd3.ExecuteReader
DDLProd.DataTextField = "p_pname"
DDLProd.DataValueField = "p_pcode"
DDLProd.DataBind()
conn.Close()
'--
DDLProd.Items.Add("Product")
'--
Case "F3"
Dim que3 As String = "select p_pcode, p_pname from product where p_stat='A' and p_advcde = " & DDLAdvert.SelectedValue & " order by p_pname asc"
Dim cmd3 As New SqlCommand(que3, conn)
conn.Open()
DDLProd.DataSource = cmd3.ExecuteReader
DDLProd.DataTextField = "p_pname"
DDLProd.DataValueField = "p_pcode"
DDLProd.DataBind()
conn.Close()
'--
DDLProd.Items.Add("Product")
'--
Case "RP"
Dim que3 As String = "select p_pcode, p_pname from RPSA.product where p_stat='A' and p_advcde = " & DDLAdvert.SelectedValue & " order by p_pname asc"
Dim cmd3 As New SqlCommand(que3, conn)
conn.Open()
DDLProd.DataSource = cmd3.ExecuteReader
DDLProd.DataTextField = "p_pname"
DDLProd.DataValueField = "p_pcode"
DDLProd.DataBind()
conn.Close()
'--
DDLProd.Items.Add("Product")
'--
'--
Case "Radio"
Dim que3 As String = "select p_pcode, p_pname from product where p_stat='A' and p_advcde = " & DDLAdvert.SelectedValue & " order by p_pname asc"
Dim cmd3 As New SqlCommand(que3, conn)
conn.Open()
DDLProd.DataSource = cmd3.ExecuteReader
DDLProd.DataTextField = "p_pname"
DDLProd.DataValueField = "p_pcode"
DDLProd.DataBind()
conn.Close()
'--
DDLProd.Items.Add("Product")
'--
'--
Case "SP"
Dim que3 As String = "select p_pcode, p_pname from SPSA.product where p_stat='A' and p_advcde = " & DDLAdvert.SelectedValue & " order by p_pname asc"
Dim cmd3 As New SqlCommand(que3, conn)
conn.Open()
DDLProd.DataSource = cmd3.ExecuteReader
DDLProd.DataTextField = "p_pname"
DDLProd.DataValueField = "p_pcode"
DDLProd.DataBind()
conn.Close()
'--
DDLProd.Items.Add("Product")
'--
End Select
End If
End Sub
Protected Sub get_prod_ver() Handles DDLProd.SelectedIndexChanged
Dim conn As New SqlConnection(Session("cs").ToString)
If Not IsPostBack = True Then
Select Case Session("platform").ToString
Case "F1"
Dim que1 As String = "select v_version +' Lenght:' + convert(varchar(10), v_spotlen) as 'Version And Lenght', v_vpcode from version where v_vpcode = (select p_pcode from product where p_pcode='" & DDLProd.SelectedValue & "')"
Dim cmd4 As New SqlCommand(que1, conn)
conn.Open()
DDLVer.DataSource = cmd4.ExecuteReader
DDLVer.DataTextField = "Version And Lenght"
DDLVer.DataValueField = "v_vpcode"
DDLVer.DataBind()
conn.Close()
DDLVer.Items.Add("Version")
'--
Case "F3"
Dim que1 As String = "select v_version +' Lenght:' + convert(varchar(10), v_spotlen) as 'Version And Lenght', v_vpcode from version where v_vpcode = (select p_pcode from product where p_pcode='" & DDLProd.SelectedValue & "')"
Dim cmd4 As New SqlCommand(que1, conn)
conn.Open()
DDLVer.DataSource = cmd4.ExecuteReader
DDLVer.DataTextField = "Version And Lenght"
DDLVer.DataValueField = "v_vpcode"
DDLVer.DataBind()
conn.Close()
DDLVer.Items.Add("Version")
'--
Case "RP"
Dim que1 As String = "select v_version +' Lenght:' + convert(varchar(10), v_spotlen) as 'Version And Lenght', v_vpcode from RPSA.version where v_vpcode = (select p_pcode from RPSA.product where p_pcode='" & DDLProd.SelectedValue & "')"
Dim cmd4 As New SqlCommand(que1, conn)
conn.Open()
DDLVer.DataSource = cmd4.ExecuteReader
DDLVer.DataTextField = "Version And Lenght"
DDLVer.DataValueField = "v_vpcode"
DDLVer.DataBind()
conn.Close()
'--
DDLVer.Items.Add("Version")
Case "Radio"
Dim que1 As String = "select v_version +' Lenght:' + convert(varchar(10), v_spotlen) as 'Version And Lenght', v_vpcode from version where v_vpcode = (select p_pcode from product where p_pcode='" & DDLProd.SelectedValue & "')"
Dim cmd4 As New SqlCommand(que1, conn)
conn.Open()
DDLVer.DataSource = cmd4.ExecuteReader
DDLVer.DataTextField = "Version And Lenght"
DDLVer.DataValueField = "v_vpcode"
DDLVer.DataBind()
conn.Close()
'--
DDLVer.Items.Add("Version")
Case "SP"
Dim que11 As String = "select v_version +' Lenght:' + convert(varchar(10), v_spotlen) as 'Version And Lenght', v_vpcode from SPSA.version where v_vpcode = (select p_pcode from RPSA.product where p_pcode='" & DDLProd.SelectedValue & "')"
Dim cmd41 As New SqlCommand(que11, conn)
conn.Open()
DDLVer.DataSource = cmd41.ExecuteReader
DDLVer.DataTextField = "Version And Lenght"
DDLVer.DataValueField = "v_vpcode"
DDLVer.DataBind()
conn.Close()
'--
DDLVer.Items.Add("Version")
End Select
Else
Select Session("platform").ToString
Case "F1"
Dim que1 As String = "select v_version +' Lenght:' + convert(varchar(10), v_spotlen) as 'Version And Lenght', v_vpcode from version where v_vpcode = (select p_pcode from product where p_pcode='" & DDLProd.SelectedValue & "')"
Dim cmd4 As New SqlCommand(que1, conn)
conn.Open()
DDLVer.ClearSelection()
DDLVer.DataSource = cmd4.ExecuteReader
DDLVer.DataTextField = "Version And Lenght"
DDLVer.DataValueField = "v_vpcode"
DDLVer.DataBind()
conn.Close()
DDLVer.Items.Add("Version")
'--
Case "F3"
Dim que1 As String = "select v_version +' Lenght:' + convert(varchar(10), v_spotlen) as 'Version And Lenght', v_vpcode from version where v_vpcode = (select p_pcode from product where p_pcode='" & DDLProd.SelectedValue & "')"
Dim cmd4 As New SqlCommand(que1, conn)
conn.Open()
DDLVer.ClearSelection()
DDLVer.DataSource = cmd4.ExecuteReader
DDLVer.DataTextField = "Version And Lenght"
DDLVer.DataValueField = "v_vpcode"
DDLVer.DataBind()
conn.Close()
DDLVer.Items.Add("Version")
'--
Case "RP"
Dim que1 As String = "select v_version +' Lenght:' + convert(varchar(10), v_spotlen) as 'Version And Lenght', v_vpcode from RPSA.version where v_vpcode = (select p_pcode from RPSA.product where p_pcode='" & DDLProd.SelectedValue & "')"
Dim cmd4 As New SqlCommand(que1, conn)
conn.Open()
DDLVer.ClearSelection()
DDLVer.DataSource = cmd4.ExecuteReader
DDLVer.DataTextField = "Version And Lenght"
DDLVer.DataValueField = "v_vpcode"
DDLVer.DataBind()
conn.Close()
'--
DDLVer.Items.Add("Version")
Case "Radio"
Dim que1 As String = "select v_version +' Lenght:' + convert(varchar(10), v_spotlen) as 'Version And Lenght', v_vpcode from version where v_vpcode = (select p_pcode from product where p_pcode='" & DDLProd.SelectedValue & "')"
Dim cmd4 As New SqlCommand(que1, conn)
conn.Open()
DDLVer.ClearSelection()
DDLVer.DataSource = cmd4.ExecuteReader
DDLVer.DataTextField = "Version And Lenght"
DDLVer.DataValueField = "v_vpcode"
DDLVer.DataBind()
conn.Close()
'--
DDLVer.Items.Add("Version")
Case "SP"
Dim que11 As String = "select v_version +' Lenght:' + convert(varchar(10), v_spotlen) as 'Version And Lenght', v_vpcode from SPSA.version where v_vpcode = (select p_pcode from RPSA.product where p_pcode='" & DDLProd.SelectedValue & "')"
Dim cmd41 As New SqlCommand(que11, conn)
conn.Open()
DDLVer.ClearSelection()
DDLVer.DataSource = cmd41.ExecuteReader
DDLVer.DataTextField = "Version And Lenght"
DDLVer.DataValueField = "v_vpcode"
DDLVer.DataBind()
conn.Close()
'--
DDLVer.Items.Add("Version")
End Select
End If
End Sub
my problem is the DDLVer is resetting its index on EVERY POSTBACKS!
if tried putting if not ispostback before the functionality nothing happens but when i ran it in debug mode, vb skips it and proceeds to end if.
second, i tried putting different conditions such as if ispostback = true, if ispostback, if not ispostback =true, but no luck..
i have been developing asp.net pages for the last 2 months and this problem with ddlver is eating 1 week on my plotted schedule..
please if there are any workaround for this let me know.
i am tired looking and reading same articles over and over again..
*bows
here is my page load event
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Page.MaintainScrollPositionOnPostBack = True
DDLVer.AppendDataBoundItems = True
If Not IsPostBack Then
Call sess_load()
Txtto.Enabled = False
Txtbg.Enabled = False
Txtencoded.Enabled = False
DDLDate.Text = Date.Today.Date
Txtfreq.Enabled = False
Txtdate.Enabled = False
Txtto1.Enabled = False
TextBox4.Enabled = False
Txtencoded.Enabled = False
End If
as you can see, im not binding any ddl on page_load event

well, what is weird with this. the ddl has the same index value so on postbacks, they will revert to the very first item. so make sure that when you load items in ddls check the ID that you will put and make sure they are unique

Related

I got an error at da.Fill(ds) command for my shopping cart project

This is my final-year mini-project. I tried to implement a shopping cart project.
But I get an error in filling details on gridview. I tried the below coding:
If Not IsPostBack Then
Dim dt As DataTable = New DataTable()
Dim dr As DataRow
dt.Columns.Add("sno")
dt.Columns.Add("productid")
dt.Columns.Add("productname")
dt.Columns.Add("price")
dt.Columns.Add("productimage")
dt.Columns.Add("cost")
dt.Columns.Add("totalcost")
If Request.QueryString("id") IsNot Nothing Then
If Session("Buyitems") Is Nothing Then
dr = dt.NewRow()
Dim mycon As String = "Data Source=Sandy-PC\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"
Dim scon As SqlConnection = New SqlConnection(mycon)
Dim myquery As String = "select * from decorativestands where Id=" & Request.QueryString("id")
Dim cmd As SqlCommand = New SqlCommand()
cmd.CommandText = myquery
cmd.Connection = scon
Dim da As SqlDataAdapter = New SqlDataAdapter()
da.SelectCommand = cmd
Dim ds As DataSet = New DataSet()
da.Fill(ds)
dr("sno") = 1
dr("productid") = ds.Tables(0).Rows(0)("productid").ToString()
dr("productname") = ds.Tables(0).Rows(0)("productname").ToString()
dr("productimage") = ds.Tables(0).Rows(0)("productimage").ToString()
dr("price") = ds.Tables(0).Rows(0)("price").ToString()
dt.Rows.Add(dr)
GridView1.DataSource = dt
GridView1.DataBind()
Session("buyitems") = dt
Else
dt = CType(Session("buyitems"), DataTable)
Dim sr As Integer
sr = dt.Rows.Count
dr = dt.NewRow()
I got "sqlexception was unhandled by user code" error.

how to select from sql database where data not in table1 need to select from table2 +asp.net vb

I have two table FixedAssetMaster_ora and FixedAssetMaster_old. What i want to do is check first if the data exist in FixedAsset_old or not. If exist, select dta from that table, if not exist, need to select data from FixedAssetMaster_ora.Currently my code always select data from FixedAssetMaster_ora.
here is my code:
Dim reccount As String = 0
Conn = New SqlConnection
Conn.ConnectionString = ConnStr
Conn.Open()
cmd = New SqlCommand
cmd.CommandText = "Select COUNT(*) FROM FixedAssetMaster_old WHERE ASSET_NUMBER=" & AssetTxt.Text & " And LOC_DEPT=" & DeptTxt.Text & " AND UNIT_NO='" & UnitNoTxt.Text & "' AND (DATEPART(MM, UPDATE_DATE) = " & nowMonth & ") AND (DATEPART(yyyy, UPDATE_DATE) =" & nowYear & ") ;"
cmd.Connection = Conn
rdmysql = cmd.ExecuteReader
If rdmysql.Read = True Then
'reccount = rdmysql.GetString(0)
reccount = Val(rdmysql.GetInt32(0))
End If
cmd.Dispose()
rdmysql.Close()
If reccount = 0 Then
strsql = "Select * FROM FixedAssetMaster_old WHERE ASSET_NUMBER=" & AssetTxt.Text & " AND LOC_DEPT=" & DeptTxt.Text & " AND UNIT_NO='" & UnitNoTxt.Text & "' And (DATEPART(MM, UPDATE_DATE) = " & nowMonth & ") And (DATEPART(yyyy, UPDATE_DATE) =" & nowYear & ") ;"
cmd.Connection = Conn
cmd.CommandText = strsql
rdmysql = cmd.ExecuteReader
If rdmysql.Read = True Then
If rdmysql.IsDBNull(rdmysql.GetOrdinal("ASSET_KEY_SEGMENT3")) = False Then LocationTxt.Text = rdmysql.GetString(rdmysql.GetOrdinal("ASSET_KEY_SEGMENT3"))
'If rdmysql.IsDBNull(rdmysql.GetOrdinal("REMARKS")) = False Then RemarksTxt.Text = rdmysql.GetString(rdmysql.GetOrdinal("REMARKS"))
'If rdmysql.IsDBNull(rdmysql.GetOrdinal("REMARKS")) = False Then DDLRemarks.Text = rdmysql.GetString(rdmysql.GetOrdinal("REMARKS"))
End If
cmd.Dispose()
rdmysql.Close()
ElseIf reccount = 1 Then
strsql = "Select * FROM FixedAssetMaster_Ora WHERE ASSET_NUMBER=" & AssetTxt.Text & " AND LOC_DEPT='" & DeptTxt.Text & " ';"
cmd.Connection = Conn
cmd.CommandText = strsql
rdmysql = cmd.ExecuteReader
If rdmysql.Read = True Then
If rdmysql.IsDBNull(rdmysql.GetOrdinal("ASSET_KEY_SEGMENT3")) = False Then LocationTxt.Text = rdmysql.GetString(rdmysql.GetOrdinal("ASSET_KEY_SEGMENT3"))
'If rdmysql.IsDBNull(rdmysql.GetOrdinal("REMARKS")) = False Then RemarksTxt.Text = rdmysql.GetString(rdmysql.GetOrdinal("REMARKS"))
'If rdmysql.IsDBNull(rdmysql.GetOrdinal("REMARKS")) = False Then DDLRemarks.Text = rdmysql.GetString(rdmysql.GetOrdinal("REMARKS"))
End If
cmd.Dispose()
rdmysql.Close()
End If
Can someone help me regarding this matter. Thank you in advance
Based on comments, I have re-worked this answer and created a quick/small Unit Test project to verify its functionality is what I understand of the problem.
<TestMethod()>
Public Sub TestGetRecordFromDatabase()
Dim targetRecord As String = "Jay2"
Dim recordIn_OldTable As Boolean = DoesRecordExistIn_Old(targetRecord)
Dim targetTable = "JayV_old"
If Not recordIn_OldTable Then
targetTable = "JayV_ora"
End If
Dim connectionString As String = "Server=MyDBServer; Database=TestingDB; Trusted_Connection=True;"
Using conn As New SqlConnection(connectionString)
conn.Open()
Dim sql As String = "SELECT * FROM " & targetTable & " where name = #name"
Using command As SqlCommand = conn.CreateCommand()
command.Parameters.Add(New SqlParameter With {.ParameterName = "name", .DbType = SqlDbType.VarChar, .Value = targetRecord})
command.Connection = conn
command.CommandType = CommandType.Text
command.CommandText = sql
Dim dataReader As SqlDataReader = command.ExecuteReader
While dataReader.Read
Dim name As String = dataReader.GetString(dataReader.GetOrdinal("name"))
Dim address As String = dataReader.GetString(dataReader.GetOrdinal("address"))
End While
End Using
conn.Close()
End Using
End Sub
Public Function DoesRecordExistIn_Old(targetRecord As String) As Boolean
Dim connectionString As String = "Server=MyDBServer; Database=TestingDB; Trusted_Connection=True;"
Using conn As New SqlConnection(connectionString)
conn.Open()
Dim sql As String = "SELECT count(*) FROM JayV_old where name = #name"
Using command As SqlCommand = conn.CreateCommand()
command.Parameters.Add(New SqlParameter With {.ParameterName = "name", .DbType = SqlDbType.VarChar, .Value = targetRecord})
command.Connection = conn
command.CommandType = CommandType.Text
command.CommandText = sql
Dim objResult As Object = command.ExecuteScalar()
Dim rowCount As Int32 = Int32.Parse(objResult)
If rowCount = 0 Then
Return False
Else
Return True
End If
End Using
conn.Close()
End Using
End Function
I re-created parts of your problem to encompass the logic of the code you have shared. I believe this works as you would like:
Check for existence of record in _old table
If it exists, load record from _old table
If it does not exist, load record from _ora table
I also re-factored the code to make it easier to maintain as the only difference between the two record retrievals is the Table Name, _old or _ora
I am not very familiar with VB but in C# you can do this as:
SqlCommand cmd = new SqlCommand();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
cmd.CommandText = "Select count(*) from FixedAssetMaster_old WHERE ASSET_NUMBER=" & AssetTxt.Text & " And LOC_DEPT=" & DeptTxt.Text & " AND UNIT_NO='" & UnitNoTxt.Text & "' AND (DATEPART(MM, UPDATE_DATE) = " & nowMonth & ") AND (DATEPART(yyyy, UPDATE_DATE) =" & nowYear & ") ;";
cmd.Connection = con;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
//Select from FixedAssetMaster_old table
}
else
{
//Select FROM FixedAssetMaster_Ora table
}

the for loop check the user and every time the both if statement is checked i want only one statement should be checked

Dim LoginUser As MembershipUser = Membership.GetUser(HttpContext.Current.User.Identity.Name)
Dim sFormat As String = "00.00"
'Dim LoginUser = "P002"
Dim sqlapt As SqlDataAdapter = New SqlDataAdapter("select * from wpchannel", connection)
Dim sqlds As DataSet = New DataSet()
Dim sqldt As DataTable = New DataTable()
sqlapt.Fill(sqlds, "wpaccess")
sqldt = sqlds.Tables(0)
'Dim sum As Double
Dim row As DataRow
For Each row In sqldt.Rows
Dim strDetail As String
strDetail = row("username")
lbluserchk.Text = strDetail
If lbluserchk.Text = LoginUser.ToString Then
Dim sqlapt1 As SqlDataAdapter = New SqlDataAdapter("select * from wpchannel where username='" + LoginUser.ToString + "'", connection)
Dim sqlds1 As DataSet = New DataSet()
Dim sqldt1 As DataTable = New DataTable()
sqlapt1.Fill(sqlds1, "wpchannel")
sqldt1 = sqlds1.Tables(0)
Dim username As String = sqldt1.Rows(0).Item(1).ToString
Dim productid As String = sqldt1.Rows(0).Item(2).ToString
lblcheck1.Text = username.ToString
lblcheck2.Text = productid
'New check
Dim da As SqlDataAdapter
Dim ds As DataSet = New DataSet()
Dim dt As DataTable = New DataTable()
Dim sqlquery2 = "select convert(varchar(10), convert(datetime, dn.curdatetime),20) as DnDate ,DN.telcoid As TelecoId,round(convert(decimal(18,2),DN.USD),2) as USD,DN.DNStatus As DnStatus,count(*) as Total,sum(round(convert(decimal(18,2),DN.USD),2)) as subtotal from DN left join MO on DN.moid = mo.linkid where mo.channeltype in (select channelid from wpchannel where username='" + LoginUser.ToString + "') Group by convert(varchar(10), convert(datetime, dn.curdatetime),20),DN.telcoid,DN.USD,DN.DNStatus order by DnDate,dn.telcoid,dnstatus,USD"
da = New SqlDataAdapter(sqlquery2, connection)
da.Fill(ds, "MO")
dt = ds.Tables(0)
ViewState("dtpayment") = dt
Session("dtpayment") = dt
GridView1.DataSource = dt
GridView1.DataBind()
GridView1.FooterRow.Cells(2).Font.Bold = True
GridView1.FooterRow.Cells(2).ForeColor = Color.Black
GridView1.FooterRow.Cells(2).Text = " Grand Total:"
GridView1.FooterRow.Cells(2).HorizontalAlign = HorizontalAlign.Right
Dim sum As Double = Convert.ToDouble(dt.Compute("SUM(" + (dt.Columns(5).ColumnName) + ")", String.Empty))
Dim footernum As Double = sum.ToString
GridView1.FooterRow.Cells(3).Text = footernum.ToString()
Dim sumnum As Double = sum.ToString
lblsum.Text = sumnum.ToString()
ViewState("dt") = dt
ViewState("sort") = "Asc"
VerifyRenderingInServerForm(GridView1)
Exit For
Else
Dim cmd As SqlCommand
Dim da1 As SqlDataAdapter
Dim ds1 As DataSet = New DataSet()
Dim dt1 As DataTable = New DataTable()
'Dim sqlquery1 = "select convert(varchar(10), convert(datetime, dn.curdatetime),20) as DnDate ,DN.telcoid As TelecoId,round(convert(decimal(18,2),DN.USD),2) as USD,DN.DNStatus As DnStatus,count(*) as Total from DN left join MO on DN.moid = mo.linkid Group by convert(varchar(10), convert(datetime, dn.curdatetime),20),DN.telcoid,DN.USD,DN.DNStatus order by DnDate DESC,dn.telcoid,dnstatus,USD"
cmd = New SqlCommand
cmd.Connection = connection
cmd.CommandText = "logReport"
cmd.CommandType = CommandType.StoredProcedure
da1 = New SqlDataAdapter(cmd)
da1.Fill(ds1, "MO")
dt1 = ds1.Tables(0)
ViewState("dtpayment") = dt1
Session("dtpayment") = dt1
GridView1.DataSource = dt1
GridView1.DataBind()
GridView1.FooterRow.Cells(2).Font.Bold = True
GridView1.FooterRow.Cells(2).ForeColor = Color.Black
GridView1.FooterRow.Cells(2).Text = " Grand Total:"
GridView1.FooterRow.Cells(2).HorizontalAlign = HorizontalAlign.Right
Dim sum As Double = Convert.ToDouble(dt1.Compute("SUM(" + dt1.Columns(2).ColumnName + ")", String.Empty)) * Convert.ToDouble(dt1.Compute("SUM(" + dt1.Columns(4).ColumnName + ")", String.Empty))
Dim footernum As Double = sum
GridView1.FooterRow.Cells(3).Text = footernum.ToString()
Dim sumnum As Double = sum.ToString
lblsum.Text = sumnum.ToString()
ViewState("dt") = dt1
ViewState("sort") = "Asc"
VerifyRenderingInServerForm(GridView1)
Continue For
End If
'Exit Sub
Next row
Catch ex As Exception
Label1.Text = ex.ToString
'jscript = ("<script language=""JavaScript"">alert(""Error! Cannot conect to the database."");</script>")
'RegisterClientScriptBlock(x, jscript)
End Try

Dropdown reverting to index on postback

so here, im using asp.net and vb as my code behind. so here is the catch, im dynamically cascading all of my values accordingly to the value of another dropdown just as cascading dropdown with ajax did. but the problem is 1 of my dropdown is reverting its index to 1 while the other dropdown is fixed with their selected value.
i have a feeling that this was caused by databinding of values to this dropdown.
below is my html markup and vb codebehind
Agency<asp:DropDownList ID="DDLAgency" runat="server" AutoPostBack="true" OnSelectedIndexChanged="agen_choose" >
<asp:ListItem Text="Agency"></asp:ListItem>
</asp:DropDownList>
<br />
Advertiser<asp:DropDownList ID="DDLAdvert" runat="server" AutoPostBack="true" OnSelectedIndexChanged = "adver_choose" >
<asp:ListItem Text="Advertiser" ></asp:ListItem>
</asp:DropDownList>
<br />
Product <asp:DropDownList ID="DDLProd" runat="server" AutoPostBack="true" OnSelectedIndexChanged="get_prod_version" OnDataBinding="lem">
<asp:ListItem Text="Product"></asp:ListItem>
</asp:DropDownList>
<br />
Version and Length <asp:DropDownList ID="DDlVer" runat="server">
<asp:ListItem Text="Version"></asp:ListItem>
</asp:DropDownList>
<br />
Program <asp:DropDownList ID="DDLProg" runat="server" >
<asp:ListItem Text="Program"></asp:ListItem> </asp:DropDownList>
<br />
here is the vb codebehind
Protected Sub get_prod_version()
Dim conn As New SqlConnection(Session("cs").ToString)
Select Case Session("platform").ToString
Case "A"
Dim que1 As String = "select v_version +' Lenght:' + convert(varchar(10), v_spotlen) as 'Version And Lenght', v_vpcode from tb.tbl where v_vpcode = (select p_pcode from sysadm.product where p_pcode='" & DDLProd.SelectedValue & "')"
Dim cmd4 As New SqlCommand(que1, conn)
conn.Open()
DDlVer.DataSource = cmd4.ExecuteReader
DDlVer.DataTextField = "Version And Lenght"
DDlVer.DataValueField = "v_vpcode"
DDlVer.DataBind()
conn.Close()
DDlVer.Items.Add("Version")
'--
Case "B"
Dim que1 As String = "select v_version +' Lenght:' + convert(varchar(10), v_spotlen) as 'Version And Lenght', v_vpcode from tb.tbl where v_vpcode = (select p_pcode from sysadm.product where p_pcode='" & DDLProd.SelectedValue & "')"
Dim cmd4 As New SqlCommand(que1, conn)
conn.Open()
DDlVer.DataSource = cmd4.ExecuteReader
DDlVer.DataTextField = "Version And Lenght"
DDlVer.DataValueField = "v_vpcode"
DDlVer.DataBind()
conn.Close()
DDlVer.Items.Add("Version")
'--
Case "C"
Dim que1 As String = "select v_version +' Lenght:' + convert(varchar(10), v_spotlen) as 'Version And Lenght', v_vpcode from tb.tbl where v_vpcode = (select p_pcode from QTVSA.product where p_pcode='" & DDLProd.SelectedValue & "')"
Dim cmd4 As New SqlCommand(que1, conn)
conn.Open()
DDlVer.DataSource = cmd4.ExecuteReader
DDlVer.DataTextField = "Version And Lenght"
DDlVer.DataValueField = "v_vpcode"
DDlVer.DataBind()
conn.Close()
'--
DDlVer.Items.Add("Version")
Case "D"
Dim que1 As String = "select v_version +' Lenght:' + convert(varchar(10), v_spotlen) as 'Version And Lenght', v_vpcode from tb.tbl where v_vpcode = (select p_pcode from SYSADM.product where p_pcode='" & DDLProd.SelectedValue & "')"
Dim cmd4 As New SqlCommand(que1, conn)
conn.Open()
DDlVer.DataSource = cmd4.ExecuteReader
DDlVer.DataTextField = "Version And Lenght"
DDlVer.DataValueField = "v_vpcode"
DDlVer.DataBind()
conn.Close()
'--
DDlVer.Items.Add("Version")
Case "E"
Dim que11 As String = "select v_version +' Lenght:' + convert(varchar(10), v_spotlen) as 'Version And Lenght', v_vpcode from tb.tbl where v_vpcode = (select p_pcode from QTVSA.product where p_pcode='" & DDLProd.SelectedValue & "')"
Dim cmd41 As New SqlCommand(que11, conn)
conn.Open()
DDlVer.DataSource = cmd41.ExecuteReader
DDlVer.DataTextField = "Version And Lenght"
DDlVer.DataValueField = "v_vpcode"
DDlVer.DataBind()
conn.Close()
'--
DDlVer.Items.Add("Version")
End Select
End Sub

My textbox is not filled with data

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
TextBox3.Text = Now()
Dim com As New SqlCommand
com.CommandType = CommandType.Text
com.CommandText = "select productname ,productid,productdescreption,price from products order by productname "
com.Connection = con
Dim ad As New SqlDataAdapter
Dim ds As New DataSet
ad.SelectCommand = com
ad.Fill(ds)
DropDownList1.DataSource = ds
DropDownList1.DataTextField = "productname"
DropDownList1.DataValueField = "productid"
'Dim ss As Integer
'ss = Convert.ToInt32(DropDownList1.DataValueField)
'DropDownList1.DataValueField = ss
DropDownList1.DataBind()
Dim com2 As New SqlCommand
com2.CommandType = CommandType.Text
com2.CommandText = "select dealername ,dealerid from dealerin order by dealername "
com2.Connection = con
Dim ad2 As New SqlDataAdapter
Dim ds2 As New DataSet
ad2.SelectCommand = com2
ad2.Fill(ds2)
DropDownList2.DataSource = ds2
DropDownList2.DataTextField = "dealername"
DropDownList2.DataValueField = "dealerid"
DropDownList2.DataBind()
'Dim com3 As New SqlCommand
'com3.CommandType = CommandType.Text
'com3.CommandText = "select distinct productname ,productid,productdescreption from products "
'com3.Connection = con
'Dim ad3 As New SqlDataAdapter
'Dim ds3 As New DataSet
'ad2.SelectCommand = com3
'ad2.Fill(ds3)
'DropDownList3.DataSource = ds3
'DropDownList3.DataTextField = "productdescreption"
'DropDownList3.DataValueField = "productid"
'DropDownList3.DataBind()
End If
End Sub
Dim dss As New DataSet
Public Function getproduct_byid(ByVal productid As Integer) As DataSet
Try
Dim com3 As New SqlCommand
com3.CommandType = CommandType.StoredProcedure
com3.CommandText = "getproduct_byid"
com3.Connection = con
'com.Parameters.AddWithValue("#productid", productid)
Dim adapter3 As New SqlDataAdapter(com3)
adapter3.Fill(dss, "product")
Return dss
Catch ex As Exception
Response.Write("error due to " & ex.Message)
End Try
End Function
Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownList1.SelectedIndexChanged
Try
getproduct_byid(DropDownList1.SelectedValue)
If dss.Tables("product").Rows.Count = 1 Then
TextBox5.Text = dss.Tables("product").Rows(0).Item("price")
End If
Catch ex As Exception
Response.Write("error due to " & ex.Message)
End Try
End Sub
Protected Sub TextBox5_TextChanged(sender As Object, e As EventArgs) Handles TextBox5.TextChanged
Try
Dim a As Double
Dim b As Double
a = TextBox4.Text
b = TextBox5.Text
TextBox6.Text = A * b
Catch ex As Exception
Response.Write("error due to " & ex.Message)
End Try
End Sub
It might be your datatable contain more than 1 row ..
Change this
If dss.Tables("product").Rows.Count = 1 Then
To
If dss.Tables("product").Rows.Count > 0 Then

Resources