虽然ASP.NET和VB.NET上有项目,但如何修复列表框中的空项目 [英] How do I fix the null items in list box although there are items on ASP.NET and VB.NET

查看:51
本文介绍了虽然ASP.NET和VB.NET上有项目,但如何修复列表框中的空项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

我是网络开发的新手。我正在使用asp.net和VB.net创建一个新的Web应用程序。



在我正在使用的页面中有一些字段,但我关心的是我使用Javascript添加代码来添加和删除列表中的项目框。值来自下拉列表。



但是当我运行后面的代码将列表框项添加到db时,它总是给我索引超出范围,尽管有超过2项。



我真的需要你的帮助。



谢谢。



我的尝试:



VB代码隐藏是:for循环中出现错误。



Hi
I'm new to web development. I'm creating a new web application using asp.net and VB.net.

in the page i'm working with there is some fields but what i'm concerned about is i added code using Javascript to add and remove items from and to list box. values are coming from a dropdownlist.

but when i run the code behind to add the listbox items to db it always gives me "index is out of range" although there more than 2 items.

I really need your help.

Thanks.

What I have tried:

The VB codebehind is : error is raised in the for loop.

Private Sub CmdSave_Click(sender As Object, e As EventArgs) Handles CmdSave.Click
        Try
            If RecSet.State = 1 Then RecSet.Close()
            SQL1 = "SELECT * FROM REF_Committees WHERE CommitteeId = " & Trim(TxtId.Text)
            RecSet.Open(SQL1, Conn, ADODB.CursorTypeEnum.adOpenKeyset)
            If Not RecSet.EOF Then
                SQL1 = "DELETE FROM REF_Committees WHERE CommitteeId = " & Trim(TxtId.Text) : Conn.Execute(SQL1)
            End If
            'GET VARIABLES
            Call GetValues(2) : Call GetValues(3)
            'INSERT RECORD
            SQL1 = "INSERT INTO REF_Committees VALUES ("
            SQL1 = SQL1 & Trim(TxtId.Text) & ",'" & Trim(TxtDesc.Text) & "','False'," & YearIdVar & "," & SemesterIdVar & ",'True','True'," & Application("SysUserIdVar") & ",'"
            SQL1 = SQL1 & Format(DateValue(Now.Date), "MM/dd/yyyy") & " " & Format(TimeValue(TimeOfDay), "hh:mm:ss tt") & "')"
            Conn.Execute(SQL1)
            Dim i As Integer = 0
            For i = 0 To LstMembers.Rows - 1
                If RecSet.State = 1 Then RecSet.Close()
                LstMembers.SelectedIndex = i : LstTitles.SelectedIndex = i
                SQL1 = "SELECT MemberId FROM REF_Members WHERE MemberDesc = '" & LstMembers.SelectedItem.Text & "'"
                RecSet.Open(SQL1, Conn, ADODB.CursorTypeEnum.adOpenKeyset)
                If Not RecSet.EOF Then
                    SQL1 = "INSERT INTO REF_Committees_Mem VALUES ("
                    SQL1 = SQL1 & Trim(TxtId.Text) & "," & RecSet(0).Value & ",'" & LstTitles.SelectedItem.Text & "','True','True'," & Application("SysUserIdVar") & ",'"
                    SQL1 = SQL1 & Format(DateValue(Now.Date), "MM/dd/yyyy") & " " & Format(TimeValue(TimeOfDay), "hh:mm:ss tt") & "')"
                    Conn.Execute(SQL1)
                End If
            Next
            Call CleanScreen()
        Catch ex As Exception

        End Try
    End Sub()

Javascript to add and remove items is :


        function AddMember() {
            var txtValue = document.getElementById("");
            var listBox = document.getElementById("");
            var option = document.createElement("OPTION");
            option.innerHTML = txtValue.value;
            option.value = txtValue.value;   
            var i;
            for (i = listBox.options.length - 1; i >= 0; i--) {
                if (listBox.options[i].value == txtValue.value) {
                    alert("This Member Is Already In The List");
                    return false;
                }
            }
            listBox.appendChild(option);
            txtValue.value = "";
            return AddTitle();
            return false;
        }
        function AddTitle() {
            var txtValue = document.getElementById("");
            var listBox = document.getElementById("");
            var option = document.createElement("OPTION");
            option.innerHTML = txtValue.value;
            option.value = txtValue.value;
            listBox.appendChild(option);
            txtValue.value = "";
            return false;
        }
        function DeleteMember() {
            var MemberList = document.getElementById("");
            var TitlesList = document.getElementById("");
            var i;
            for (i = MemberList.options.length - 1; i >= 0; i--) {
                if (MemberList.options[i].selected == true) {
                    MemberList.remove(i);
                    TitlesList.remove(i);
                }
            }
            return;
        }

推荐答案

这将在很大程度上取决于您的数据 - 我们根本无法访问这些数据。并且taht意味着我们不能为你做太多,如果有的话。



所以,这将取决于你。

幸运,你有一个工具可以帮助你找到正在发生的事情:调试器。你如何使用它取决于你的编译器系统,但是一个快速的谷歌用于你的IDE名称和调试器应该给你你需要的信息。



放一个断点在函数的第一行,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!





并为自己做一个大忙:不要那样做数据库代码!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

This is going to depend heavily on your data - which we have no access to at all. And taht means we can't do much for you, if anything.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


And do yourself a big favour: Don't do database code like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期做备份,不是吗?

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?


这篇关于虽然ASP.NET和VB.NET上有项目,但如何修复列表框中的空项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆