我的SQL查询中没有选定的值 [英] No selected value in my sql query

查看:75
本文介绍了我的SQL查询中没有选定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我是新手.net

我有一个问题,当我调试我的代码时,没有价值在尝试执行sql查询时。 cbContact.text中的值为空。 cbContact是一个下拉列表,我已经选择了所选的值。我已经尝试将属性更改为.selectedvalue但仍然相同。欣赏你们的建议。请找到我的编码如下。



 受保护的  Sub  btnSave_Click( ByVal  sender  As   Object  ByVal  e  As  System.EventArgs) Handles  btnSave.Click 
如果 txtprojectname.Text = 然后
cf.messagebox( 请输入项目名称!
txtprojectname.Focus()
退出 Sub
结束 如果

Dim StrSQL As StringBuilder

如果 txtprojectname.Text<> 然后
StrSQL .Remove( 0 ,StrSQL.Length)
StrSQL.Append( INSERT INTO tblorderdetails(Customer,SSIStaff,PONumber,CONumber,ProjectName,DateOrderReceived)VALUES(
StrSQL.Append( ' + cbCustCode.Text + '
StrSQL.Append( ,' + cbContact.Text + '
StrSQL.Append( ,' + txtponum.Text + '
StrSQL.Append(< span class =code-string> ,' + txtconum.Text + '
StrSQL.Append( ,' + txtprojectname.Text + '
< span class =code-keyword>如果 IsNothing(dpOrderReceive.SelectedValue)那么
StrSQL.Append( ,' +格式( CDate (dpOrderReceive.SelectedValue), yyyy-MM-dd) + ')
其他
StrSQL.Append( ,Null)
结束 如果

cf.runSQLComm(StrSQL.ToString)
' cf.addLog( Session(LoginID)。ToString.Trim,Create+ i.ToString)
cf.messagebox( 记录创建!
退出 Sub
Else
退出 Sub
结束 如果
结束 Sub





至于cbContact,这里是编码。我从表中查询值。



 受保护  Sub  Page_Load( ByVal  sender  As   Object  ByVal  e  As  System.EventArgs)句柄  .Load 
Session.LCID = 2057

.MaintainScrollPositionOnPostBack = True

BindComboBoxList()

结束 Sub

< span class =code-keyword> Sub BindComboBoxList()
tblData = cf.GetDataTable( 从tblcontactperson O中选择ContactPerson rder by ContactPerson
cbContact.Items.Clear()
cbContact.Items.Add(
对于 每个 r As DataRow In tblData.Rows
cbContact.Items.Add(r( 0 )。ToString.Trim)
下一步
结束 Sub

解决方案

您必须添加

 BindComboBoxList()





里面

 如果  IsPostBack 那么 

BindComboBoxList()

结束 IF


关于它的文章..

为什么DropDownList SelectedValue在SelectedIndexChanged事件中不起作用? [ ^ ]



确保,如果你绑定页面加载事件中的下拉列表然后将其放入内部

<前一个=c#> 如果(!IsPostBack)
{
// bind dropdown
}


Hi All

I am new to .net
I have a problem where when I debug my code, there is no value when trying to execute sql query. The value in cbContact.text is blank. cbContact is a dropdownlist where I already choose the selected value. I already did try change the property to .selectedvalue but still same. Appreciate on you guys advise. Please find my coding as below.

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If txtprojectname.Text = "" Then
            cf.messagebox("Please key in Project Name !", Me)
            txtprojectname.Focus()
            Exit Sub
        End If

        Dim StrSQL As New StringBuilder

        If txtprojectname.Text <> "" Then
            StrSQL.Remove(0, StrSQL.Length)
            StrSQL.Append("INSERT INTO tblorderdetails(Customer,SSIStaff,PONumber,CONumber,ProjectName,DateOrderReceived) VALUES (")
            StrSQL.Append("'" + cbCustCode.Text + "'")
            StrSQL.Append(",'" + cbContact.Text + "'")
            StrSQL.Append(",'" + txtponum.Text + "'")
            StrSQL.Append(",'" + txtconum.Text + "'")
            StrSQL.Append(",'" + txtprojectname.Text + "'")
            If Not IsNothing(dpOrderReceive.SelectedValue) Then
                StrSQL.Append(",'" + Format(CDate(dpOrderReceive.SelectedValue), "yyyy-MM-dd") + "')")
            Else
                StrSQL.Append(",Null)")
            End If

            cf.runSQLComm(StrSQL.ToString)
            'cf.addLog(Session("LoginID").ToString.Trim, "Create " + i.ToString)
            cf.messagebox("Record created !", Me)
            Exit Sub
        Else
            Exit Sub
        End If
    End Sub



As for the cbContact here is the coding. I query the value from a table.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Session.LCID = 2057

        Me.MaintainScrollPositionOnPostBack = True

        BindComboBoxList()

End Sub

Sub BindComboBoxList()
        tblData = cf.GetDataTable("Select ContactPerson from tblcontactperson Order by ContactPerson")
        cbContact.Items.Clear()
        cbContact.Items.Add("")
        For Each r As DataRow In tblData.Rows
            cbContact.Items.Add(r(0).ToString.Trim)
        Next
 End Sub

解决方案

You have to add your

BindComboBoxList()



inside

If not IsPostBack Then

BindComboBoxList()

End IF


Article about it..
Why DropDownList SelectedValue Does Not Work Inside SelectedIndexChanged Event?[^]

make sure,if you are binding the Dropdown list in page load event then place it inside

if(!IsPostBack)
{
//bind dropdown
}


这篇关于我的SQL查询中没有选定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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