Ron Ronmonstererer Ankrah Ron Ronmonstererer Ankrah - 4 months ago 6
Vb.net Question

Unchecked IDs displaying wrong ID.

While I was debugging, when there's an checked report, it shows its correct ID. But when there's an unchecked report below, it shows the ID of the checked report above it and it follows for every unchecked report below the checked one. The problem is in my query, and after changing some keywords, in the WHILE and SELECT statements, it still shows the wrong ID. Where could the problem be coming from?

If chk.Checked = True Then
rpti = CType(oDataGridItem.FindControl("lblReportID"), Label).Text

valsql2 = "INSERT INTO CompanyReportListTable(CompanyID, ReportID) Values (valCompanyId, rpti) WHERE NOT EXISTS(SELECT * FROM CompanyReportListTable WHERE ReportID <> " + rpti + " and CompanyID <> " + valCompanyId + ")"
count += 1
rptn = CType(oDataGridItem.FindControl("lblrptName"), Label).Text
oExArgs.Add(rptn)
'Response.Write(rpti + " - " + rptn + "<br/>")
Else
valsql2 = "DELETE FROM CompanyReportListTable WHERE EXISTS(Select * FROM CompanyReportListTable WHERE ReportID = " + rpti + " and CompanyID = " + valCompanyId + ")"
End If

Answer

Take a look at your If block:

' Set the rpti value
rpti = CType(oDataGridItem.FindControl("lblReportID"), Label).Text
' Use the rpti value
valsql2 = "INSERT INTO CompanyReportListTable(CompanyID, ReportID) Values (valCompanyId, rpti) WHERE NOT EXISTS(SELECT * FROM CompanyReportListTable WHERE ReportID  <> " + rpti + " and CompanyID <> " + valCompanyId + ")"

And your Else block:

' Use the rpti value
valsql2 = "DELETE FROM CompanyReportListTable WHERE EXISTS(Select * FROM CompanyReportListTable WHERE ReportID  = " + rpti + " and CompanyID = " + valCompanyId + ")"

Since you never set the rpti value in the Else block, it's going to have whatever value it already had.

Just set the value to what you need before you use it.