Dropdown reverting to index on postback - asp.net

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

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
}

Getting NULL Value errors even when there are no null values in the database

We have an ASPX site that displays charts given stats captured by a server onto a database. Recently, it's giving us a "There is no row at position 0." error and "Index was outside the bounds of the array. " error. We usually get this error when some fields failed to be filled by the capture, thus giving us NULL values. But today, it's giving us these errors despite not having NULL values.
Here are two functions that give us the error:
"Index was outside the bounds of the array" (at this line: ctrDMX(ctr) = "")
Public Function Bar_Task_Cat() As String
Dim Conn As SqlConnection = New SqlConnection(MSSQLstring)
Conn.Open()
Dim strUserCode = CType(Session("usrCd"), String)
Dim SQLstring As String
If strUserCode = "ADMIN" Then
SQLstring = "SELECT COUNT(AskAOID) AS credits, CatCode AS category, ProductCode " _
& "FROM dbo.AskAO_Stats " _
& "WHERE (FirstResponse BETWEEN '" & dpFrom.SelectedDate.ToString & "' AND '" & dpTo.SelectedDate.ToString & "') " _
& "AND (FirstResponse IS NOT NULL) AND (CatCode IS NOT NULL) AND (ProductCode IS NOT NULL) " _
& "GROUP BY ProductCode, CatCode " _
& "ORDER BY CatCode"
Else
SQLstring = "SELECT COUNT(AskAOID) AS credits, CatCode AS category, ProductCode " _
& "FROM dbo.AskAO_Stats " _
& "WHERE (FirstResponse BETWEEN '" & dpFrom.SelectedDate.ToString & "' AND '" & dpTo.SelectedDate.ToString & "') " _
& "AND (Deferred = '" & strUserCode & "') " _
& "AND (FirstResponse IS NOT NULL) AND (CatCode IS NOT NULL) AND (ProductCode IS NOT NULL) " _
& "GROUP BY ProductCode, CatCode " _
& "ORDER BY CatCode"
End If
Dim SQLcmd As SqlCommand = New SqlCommand(SQLstring, Conn)
Dim DA As New SqlDataAdapter(SQLstring, Conn)
Dim DS As New DataSet
DA.Fill(DS)
Conn.Close()
Dim Def_Color_t(12) As String
Def_Color_t(0) = "AFD8F8"
Def_Color_t(1) = "F6BD0F"
Def_Color_t(2) = "8BBA00"
Def_Color_t(3) = "FF8E46"
Def_Color_t(4) = "008E8E"
Def_Color_t(5) = "D64646"
Def_Color_t(6) = "8E468E"
Def_Color_t(7) = "588526"
Def_Color_t(8) = "B3AA00"
Def_Color_t(9) = "008ED6"
Def_Color_t(10) = "9D080D"
Def_Color_t(11) = "A186BE"
Def_Color_t(12) = "AFD8F8"
Dim ctr As Integer = 0
Dim ctrD As Integer = 0
Dim ctrM As Integer = 0
Dim ctrF As Integer = 0
Dim ctrA As Integer = 0
Dim ctrO As Integer = 0
Dim m_ctr As Integer = 0
Dim m1_ctr As Integer = 0
Dim strXML As String
Dim Cat As String
Dim OldCat As String
Dim strCatName As String
Dim ProdCode As String
'counters for Products
Dim ctrDMX(12), ctrMHM(12), ctrFCX(12), ctrAPM(12), ctrPOR(12), ctrONL(12), ctrDVX(12), ctrOTH(12) As String
'counters for Categories
'Dim ctrOL(12), ctrAR(12), ctrPR(12), ctrST(12), ctrPQ(12), ctrFW(12), ctrOO(12) As String
Dim ctrAR(12), ctrDA(12), ctrDB(12), ctrDC(12), ctrDD(12), ctrDE(12), ctrDF(12), ctrDG(12), ctrDH(12), ctrDI(12), ctrDJ(12), ctrDK(12) As String
Dim ctrDL(12), ctrDM(12), ctrDN(12), ctrDO(12), ctrDP(12), ctrDQ(12), ctrFW(12), ctrOL(12), ctrOO(12), ctrPR(12), ctrPQ(12), ctrST(12) As String
strXML = ""
Cat = ""
OldCat = ""
strCatName = ""
strXML = strXML & "<chart palette='2' labelDisplay='Rotate' slantLabels='1' showBorder='0' bgColor='FFFFFF,FFFFFF' showPercentValues='1' showPercentInToolTip='1' stack100Percent='1' caption='Percentage of Tasks by Category by Product' shownames='1' showvalues='0' showSum='1' decimals='0' useRoundEdges='1'>"
strXML = strXML & "<categories>"
Do Until ctr = DS.Tables(0).Rows.Count
Cat = DS.Tables(0).Rows(ctr)("category").ToString
If Cat <> OldCat Then
Select Case DS.Tables(0).Rows(ctr)("category").ToString
Case "AR"
strCatName = "AR"
Case "DA"
strCatName = "DA"
Case "DB"
strCatName = "DB"
Case "DC"
strCatName = "DC"
Case "DD"
strCatName = "DD"
Case "DE"
strCatName = "DE"
Case "DF"
strCatName = "DF"
Case "DG"
strCatName = "DG"
Case "DH"
strCatName = "DH"
Case "DI"
strCatName = "DI"
Case "DJ"
strCatName = "DJ"
Case "DK"
strCatName = "DK"
Case "DL"
strCatName = "DL"
Case "DM"
strCatName = "DM"
Case "DN"
strCatName = "DN"
Case "DO"
strCatName = "DO"
Case "DP"
strCatName = "DP"
Case "DQ"
strCatName = "DQ"
Case "FW"
strCatName = "FW"
Case "OL"
strCatName = "OL"
Case "OO"
strCatName = "OO"
Case "PR"
strCatName = "PR"
Case "PQ"
strCatName = "PQ"
Case "ST"
strCatName = "ST"
End Select
strXML = strXML & "<category label='" & strCatName & "' value='" & DS.Tables(0).Rows(ctr)("credits").ToString & "' />"
m_ctr = m_ctr + 1
End If
OldCat = Cat
ctr = ctr + 1
Loop
strXML = strXML & "</categories>"
'initialize
ctr = 0
Do Until ctr = m_ctr
ctrDMX(ctr) = "<set value='0' />" 'This line gives the error
ctrFCX(ctr) = "<set value='0' />"
ctrMHM(ctr) = "<set value='0' />"
ctrPOR(ctr) = "<set value='0' />"
ctrONL(ctr) = "<set value='0' />"
ctrAPM(ctr) = "<set value='0' />"
ctrDVX(ctr) = "<set value='0' />"
ctrOTH(ctr) = "<set value='0' />"
ctr = ctr + 1
Loop
ctr = 0
Cat = ""
OldCat = DS.Tables(0).Rows(0)("category").ToString
Do Until ctr = DS.Tables(0).Rows.Count
ProdCode = DS.Tables(0).Rows(ctr)("ProductCode").ToString
Cat = DS.Tables(0).Rows(ctr)("category").ToString
If Cat <> OldCat Then
m1_ctr = m1_ctr + 1
End If
Select Case ProdCode
Case "DMX"
ctrDMX(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("credits").ToString & "' />"
Case "FCX"
ctrFCX(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("credits").ToString & "' />"
Case "MHM"
ctrMHM(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("credits").ToString & "' />"
Case "POR"
ctrPOR(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("credits").ToString & "' />"
Case "ONL"
ctrONL(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("credits").ToString & "' />"
Case "APM"
ctrAPM(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("credits").ToString & "' />"
Case "DVX"
ctrDVX(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("credits").ToString & "' />"
Case "OTH"
ctrOTH(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("credits").ToString & "' />"
End Select
OldCat = Cat
ctr = ctr + 1
Loop
ctr = 0
strXML = strXML & "<dataset seriesName='AMS Device' color='AFD8F8' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctrDMX(ctr)
ctr = ctr + 1
Loop
strXML = strXML & "</dataset>"
ctr = 0
strXML = strXML & "<dataset seriesName='Field Communicator' color='8BBA00' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctrFCX(ctr)
ctr = ctr + 1
Loop
strXML = strXML & "</dataset>"
ctr = 0
strXML = strXML & "<dataset seriesName='Machinery Health' color='F6BD0F' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctrMHM(ctr)
ctr = ctr + 1
Loop
strXML = strXML & "</dataset>"
ctr = 0
strXML = strXML & "<dataset seriesName='Portables' color='AFD8F8' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctrPOR(ctr)
ctr = ctr + 1
Loop
strXML = strXML & "</dataset>"
ctr = 0
strXML = strXML & "<dataset seriesName='Online' color='A186BE' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctrONL(ctr)
ctr = ctr + 1
Loop
strXML = strXML & "</dataset>"
ctr = 0
strXML = strXML & "<dataset seriesName='Asset Portal' color='FF8E46' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctrAPM(ctr)
ctr = ctr + 1
Loop
strXML = strXML & "</dataset>"
ctr = 0
strXML = strXML & "<dataset seriesName='DeltaV' color='9D080D' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctrDVX(ctr)
ctr = ctr + 1
Loop
strXML = strXML & "</dataset>"
ctr = 0
strXML = strXML & "<dataset seriesName='Others' color='008E8E' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctrOTH(ctr)
ctr = ctr + 1
Loop
strXML = strXML & "</dataset>"
strXML = strXML & "</chart>"
'Create the chart - Column 3D Chart with data from strXML variable using dataXML method
Return RenderChartHTML("Charts/NewCharts/StackedColumn3D.swf", "", strXML, "myNext", "450", "500", False)
'for gauge
End Function
"There is no row at position 0" (at this line: OldMonth = DS.Tables(0).Rows(0)("Month").ToString)
Public Function FRTChart() As String
Dim Conn As SqlConnection = New SqlConnection(MSSQLstring)
Conn.Open()
Dim strUserCode = CType(Session("usrCd"), String)
Dim SQLstring As String
If strUserCode = "ADMIN" Then
SQLstring = "SELECT Count(FirstResponse) AS TotalQueries, SUBSTRING(CONVERT(VARCHAR(11), FirstResponse, 113), 4, 8) AS Month, RIGHT(CONVERT(VARCHAR(7), FirstResponse , 120), 2) AS Month_Order, CAST(YEAR(FirstResponse) AS VARCHAR(4)) AS Year, " _
& "SUM(CASE WHEN DATEDIFF(HOUR, Date_Sent, FirstResponse) <= 2 THEN 1 ELSE 0 END) AS LessThan2Hours, " _
& "SUM(CASE WHEN DATEDIFF(HOUR, Date_Sent, FirstResponse) between 3 AND 24 THEN 1 ELSE 0 END) AS LessThan24Hours, " _
& "SUM(CASE WHEN DATEDIFF(HOUR, Date_Sent, FirstResponse) between 25 AND 48 THEN 1 ELSE 0 END) AS LessThan48Hours, " _
& "SUM(CASE WHEN DATEDIFF(HOUR, Date_Sent, FirstResponse) between 49 AND 72 THEN 1 ELSE 0 END) AS LessThan72Hours, " _
& "SUM(CASE WHEN DATEDIFF(HOUR, Date_Sent, FirstResponse) between 73 AND 96 THEN 1 ELSE 0 END) AS LessThan96Hours, " _
& "SUM(CASE WHEN DATEDIFF(HOUR, Date_Sent, FirstResponse) > 96 THEN 1 ELSE 0 END) AS Over96Hours " _
& "FROM [AskAO].dbo.AskAO_Stats " _
& "WHERE (FirstResponse BETWEEN '" & dpFrom.SelectedDate.ToString & "' AND '" & dpTo.SelectedDate.ToString & "') " _
& "AND (Status = 'CLOSED' OR Status = 'ACTIVE') " _
& "AND (FirstResponse IS NOT NULL) " _
& "GROUP BY SUBSTRING(CONVERT(VARCHAR(11), FirstResponse , 113), 4, 8), CONVERT(VARCHAR(7), FirstResponse , 120), CAST(YEAR(FirstResponse) AS VARCHAR(4)) " _
& "ORDER BY Year, Month_Order"
Else
SQLstring = "SELECT Count(FirstResponse) AS TotalQueries, SUBSTRING(CONVERT(VARCHAR(11), FirstResponse, 113), 4, 8) AS Month, RIGHT(CONVERT(VARCHAR(7), FirstResponse , 120), 2) AS Month_Order, CAST(YEAR(FirstResponse) AS VARCHAR(4)) AS Year, " _
& "SUM(CASE WHEN DATEDIFF(HOUR, Date_Sent, FirstResponse) <= 2 THEN 1 ELSE 0 END) AS LessThan2Hours, " _
& "SUM(CASE WHEN DATEDIFF(HOUR, Date_Sent, FirstResponse) between 3 AND 24 THEN 1 ELSE 0 END) AS LessThan24Hours, " _
& "SUM(CASE WHEN DATEDIFF(HOUR, Date_Sent, FirstResponse) between 25 AND 48 THEN 1 ELSE 0 END) AS LessThan48Hours, " _
& "SUM(CASE WHEN DATEDIFF(HOUR, Date_Sent, FirstResponse) between 49 AND 72 THEN 1 ELSE 0 END) AS LessThan72Hours, " _
& "SUM(CASE WHEN DATEDIFF(HOUR, Date_Sent, FirstResponse) between 73 AND 96 THEN 1 ELSE 0 END) AS LessThan96Hours, " _
& "SUM(CASE WHEN DATEDIFF(HOUR, Date_Sent, FirstResponse) > 96 THEN 1 ELSE 0 END) AS Over96Hours " _
& "FROM [AskAO].dbo.AskAO_Stats " _
& "WHERE (FirstResponse BETWEEN '" & dpFrom.SelectedDate.ToString & "' AND '" & dpTo.SelectedDate.ToString & "') " _
& "AND (Status = 'CLOSED' OR Status = 'ACTIVE') AND (Deferred = '" & strUserCode & "') " _
& "AND (FirstResponse IS NOT NULL) " _
& "GROUP BY SUBSTRING(CONVERT(VARCHAR(11), FirstResponse , 113), 4, 8), CONVERT(VARCHAR(7), FirstResponse , 120), CAST(YEAR(FirstResponse) AS VARCHAR(4)) " _
& "ORDER BY Year, Month_Order"
End If
Dim SQLcmd As SqlCommand = New SqlCommand(SQLstring, Conn)
Dim DA As New SqlDataAdapter(SQLstring, Conn)
Dim DS As New DataSet
DA.Fill(DS)
Dim Def_Color_t(12) As String
Def_Color_t(0) = "AFD8F8"
Def_Color_t(1) = "F6BD0F"
Def_Color_t(2) = "8BBA00"
Def_Color_t(3) = "FF8E46"
Def_Color_t(4) = "008E8E"
Def_Color_t(5) = "D64646"
Def_Color_t(6) = "8E468E"
Def_Color_t(7) = "588526"
Def_Color_t(8) = "B3AA00"
Def_Color_t(9) = "008ED6"
Def_Color_t(10) = "9D080D"
Def_Color_t(11) = "A186BE"
Def_Color_t(12) = "AFD8F8"
Dim ctr As Integer = 0
Dim ctrD As Integer = 0
Dim ctrM As Integer = 0
Dim ctrF As Integer = 0
Dim ctrA As Integer = 0
Dim ctrO As Integer = 0
Dim m_ctr As Integer = 0
Dim m1_ctr As Integer = 0
Dim strXML As String
Dim CatMonth As String
Dim OldMonth As String
Dim ctr2(12), ctr24(12), ctr48(12), ctr72(12), ctr96(12), ctro96(12) As String
strXML = ""
CatMonth = ""
OldMonth = ""
strXML = strXML & "<chart palette='2' legendPosition='RIGHT' labelDisplay='Rotate' slantLabels='1' showBorder='0' bgColor='FFFFFF,FFFFFF' caption='First Response Time' shownames='1' showvalues='0' showSum='1' decimals='0' useRoundEdges='1' showPercentValues='1' showPercentInToolTip='1'>"
strXML = strXML & "<categories>"
Do Until ctr = DS.Tables(0).Rows.Count
CatMonth = DS.Tables(0).Rows(ctr)("Month").ToString
If CatMonth <> OldMonth Then
strXML = strXML & "<category label='" & DS.Tables(0).Rows(ctr)("Month").ToString & "' value='" & DS.Tables(0).Rows(ctr)("TotalQueries").ToString & "' />"
m_ctr = m_ctr + 1
End If
OldMonth = CatMonth
ctr = ctr + 1
Loop
strXML = strXML & "</categories>"
'initialize
ctr = 0
Do Until ctr = m_ctr
ctr2(ctr) = "<set value='0' />"
ctr24(ctr) = "<set value='0' />"
ctr48(ctr) = "<set value='0' />"
ctr72(ctr) = "<set value='0' />"
ctr96(ctr) = "<set value='0' />"
ctro96(ctr) = "<set value='0' />"
ctr = ctr + 1
Loop
ctr = 0
CatMonth = ""
OldMonth = DS.Tables(0).Rows(0)("Month").ToString 'This line gives the error
Do Until ctr = DS.Tables(0).Rows.Count
'ProdCode = DS.Tables(0).Rows(ctr)("ProductCode").ToString
'respTime = DS.Tables(0).Rows(ctr)("ResponseTime")
CatMonth = DS.Tables(0).Rows(ctr)("Month").ToString
If CatMonth <> OldMonth Then
m1_ctr = m1_ctr + 1
End If
ctr2(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("LessThan2Hours").ToString & "' />"
ctr24(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("LessThan24Hours").ToString & "' />"
ctr48(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("LessThan48Hours").ToString & "' />"
ctr72(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("LessThan72Hours").ToString & "' />"
ctr96(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("LessThan96Hours").ToString & "' />"
ctro96(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("Over96Hours").ToString & "' />"
OldMonth = CatMonth
ctr = ctr + 1
Loop
ctr = 0
strXML = strXML & "<dataset seriesName='2 Hrs' color='AFD8F8' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctr2(ctr)
ctr = ctr + 1
Loop
strXML = strXML & "</dataset>"
ctr = 0
strXML = strXML & "<dataset seriesName='24 Hrs' color='8BBA00' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctr24(ctr)
ctr = ctr + 1
Loop
strXML = strXML & "</dataset>"
ctr = 0
strXML = strXML & "<dataset seriesName='24 - 48 Hrs' color='F6BD0F' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctr48(ctr)
ctr = ctr + 1
Loop
strXML = strXML & "</dataset>"
ctr = 0
strXML = strXML & "<dataset seriesName='48 - 72 Hrs' color='008E8E' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctr72(ctr)
ctr = ctr + 1
Loop
strXML = strXML & "</dataset>"
ctr = 0
strXML = strXML & "<dataset seriesName='72 - 96 Hrs' color='A186BE' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctr96(ctr)
ctr = ctr + 1
Loop
strXML = strXML & "</dataset>"
ctr = 0
strXML = strXML & "<dataset seriesName='Over 96 Hrs' color='FF8E46' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctro96(ctr)
ctr = ctr + 1
Loop
strXML = strXML & "</dataset>"
strXML = strXML & "</chart>"
'Create the chart - Column 3D Chart with data from strXML variable using dataXML method
Return RenderChartHTML("Charts/NewCharts/StackedColumn3D.swf", "", strXML, "myNext", "450", "370", False)
'for gauge
End Function
When I checked the SQL results for the "Admin" account, these are the results:
There are no NULL values, yet I get those errors in strUserCode = "ADMIN" :( it works fine in Individual users
Please help :( Thanks in advance!
An "Index was outside the bounds of the array" error has nothing to do with NULL database values, it has to do with you trying to access an index in an array that is outside the array length. One thing to keep in mind is that arrays are 0 index based, which means that the first item in the array will be index 0, and then the last item in an array with length of 3 will have an index of 2.
Check anywhere that is accessing a collection using its index, and makes sure that you don't have any code that will step outside of that last index. Keep in mind that when using loops, the last index will be the Count - 1.
In your first code block that gives the out of bounds exception:
Do Until ctr = m_ctr
ctrDMX(ctr) = "<set value='0' />" 'This line gives the error
The ctrDMX array is only initialized to contain a max of 13 items, but m_ctr is set based on your SQL query results #2 which has many more values (my guess is "admin" user sees more categories?). Since your values that are going into this array are dynamically generated from the database, you may either have to look at ReDim'ing your arrays based on the DB results, or just switch to use something easier like a List(of String) where you can call .Add(myString) without worrying about the indexes.
For the 2nd exception, you have 0 rows in the table when you are getting to the line:
OldMonth = DS.Tables(0).Rows(0)("Month").ToString 'This line gives the error
so check the make sure you have at least one row before accessing:
If DS.Tables(0).Rows(0) >= 1 Then
OldMonth = DS.Tables(0).Rows(0)("Month").ToString 'This line gives the error
Else
OldMonth = String.Empty
End If
One more very important issue that was not in your original question, do NOT build your SQL strings by concatenating string together. This creates a large SQL Injection vulnerability on your website. Use SQL Parameters for your filter values. Instead of having things like "WHERE (FirstResponse BETWEEN '" & dpFrom.SelectedDate.ToString & "' AND '" & dpTo.SelectedDate.ToString & "') " in your SQL String, change it to "WHERE (FirstResponse BETWEEN #dateFrom AND #dateTo) "
and then add parameter values to your SQLCommand: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters(v=vs.110).aspx
Dim SQLcmd As SqlCommand = New SqlCommand(SQLstring, Conn)
SQLcmd.Parameters.AddWithValue("#dateFrom", dpFrom.SelectedDate)
SQLcmd.Parameters.AddWithValue("#dateTo", dpTo.SelectedDate)

ddl is reverting on postbacks

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

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