英文:
Display Insert statement if RadioButtonList has a selected value of "Yes", otherwise display update statement. This is not working correctly
问题
以下是翻译好的部分:
我已经在网上搜索过,以及在这个论坛上寻找答案,但找不到适用于我的情况的解决方案。
我有一个包含“是”或“否”值的 RadioButtonList。
我们的要求是,如果 RadioButtonList 的值为“是”,则显示插入语句。否则,显示更新语句。
如果我将 IF 语句从“是”更改为“否”,则始终会显示更新语句。我可能错在哪里?
<asp:RadioButtonList ID="RadioButtonList1" RepeatDirection="Horizontal" runat="server">
<asp:ListItem Value="Yes"></asp:ListItem>
<asp:ListItem Value="No"></asp:ListItem>
</asp:RadioButtonList>
If VetoRadioButtonList1.SelectedIndex = 0 Then
ddlNumber.SelectedValue.ToString()
ddlNumber.Items.Clear()
Dim com As SqlCommand = New SqlCommand()
command.Connection = sqlCon
command.CommandType = CommandType.Text
command.CommandText = "insert into WaterVolume(WaterSizes)values(@tsizes)"
command.Parameters.Clear()
command.Parameters.AddWithValue("@tsizes", txt_watersizes.Text)
If sqlCon.State = ConnectionState.Closed Then sqlCon.Open()
com.ExecuteNonQuery()
lblSuccess.ForeColor = Color.Green
lblSuccess.Text = "Records successfully inserted"
sqlCon.Close()
Else
ddlNumber.SelectedValue.ToString()
ddlNumber.Items.Clear()
Dim com As SqlCommand = New SqlCommand()
com.Connection = sqlCon
com.CommandType = CommandType.Text
com.CommandText = "Update table2 set firstname=@firstname, lastname=@lastname where empId = @id"
com.Parameters.Clear()
com.Parameters.AddWithValue("@id", empID.Text)
com.Parameters.AddWithValue("@firstname", txt_firstname.Text)
com.Parameters.AddWithValue("@lastname", txt_lsttname.Text)
If sqlCon.State = ConnectionState.Closed Then sqlCon.Open()
com.ExecuteNonQuery()
lblSuccess.ForeColor = Color.Green
lblSuccess.Text = "Records successfully updated"
sqlCon.Close()
End If
希望这对您有所帮助。
英文:
I have searched the web, as well as this forum for answers but cannot find any solution that works for my situation.
I have a RadioButtonList with a Yes or No value.
Our requirement is to display an insert statement if RadioButtonList has a value of Yes. Otherwise, display an update statement.
The following is always displaying an update statement if I switch the IF statement from Yes to No.
Any ideas what I could be wrong?
<asp:RadioButtonList ID="RadioButtonList1" RepeatDirection="Horizontal" runat="server">
<asp:ListItem Value="Yes"></asp:ListItem>
<asp:ListItem Value="No"></asp:ListItem>
</asp:RadioButtonList>
If VetoRadioButtonList1.SelectedIndex = 0 THen
ddlNumber.SelectedValue.ToString()
ddlNumber.Items.Clear()
Dim com As SqlCommand = New SqlCommand()
command.Connection = sqlCon
command.CommandType = CommandType.Text
command.CommandText = "insert into WaterVolume(WaterSizes)values(@tsizes)"
command.Parameters.Clear()
command.Parameters.AddWithValue("@tsizes", txt_watersizes.Text)
If sqlCon.State = ConnectionState.Closed Then sqlCon.Open()
com.ExecuteNonQuery()
lblSuccess.ForeColor = Color.Green
lblSuccess.Text = "Records successfully inserted"
sqlCon.Close()
else
ddlNumber.SelectedValue.ToString()
ddlNumber.Items.Clear()
Dim com As SqlCommand = New SqlCommand()
com.Connection = sqlCon
com.CommandType = CommandType.Text
com.CommandText = "Update table2 set firstname=@firstname, lastname=@lastname where empId = @id"
com.Parameters.Clear()
com.Parameters.AddWithValue("@id", empID.Text)
com.Parameters.AddWithValue("@firstname", txt_firstname.Text)
com.Parameters.AddWithValue("@lastname", txt_lsttname.Text)
If sqlCon.State = ConnectionState.Closed Then sqlCon.Open()
com.ExecuteNonQuery()
lblSuccess.ForeColor = Color.Green
lblSuccess.Text = "Records successfully updated"
sqlCon.Close()
End If
答案1
得分: 0
你发布的RB列表的“id”与你的代码不同。但是,我猜测你可能在使用更新面板或类似的东西。并且你没有显示这段代码应该如何运行(按钮点击?)。
另外,我不知道连接对象是从哪里来的,但不要尝试创建某种持久连接对象(不要这样做!!)。
所以,代码模式如下:
Protected Sub Button1_Click(sender As Object, e As EventArgs)
If VetoRadioButtonList1.SelectedIndex = 0 Then
Dim strSQL As String =
"insert into WaterVolume (WaterSizes) values(@tsizes)"
Dim cmdSQL As New SqlCommand(strSQL)
cmdSQL.Parameters.Add("@tsizes", SqlDbType.Int).Value = txt_watersizes.Text
MyrstPN(cmdSQL)
lblSuccess.ForeColor = Color.Green
lblSuccess.Text = "Records successfully inserted"
Else
Dim strSQL As String =
"Update table2 set firstname = @firstname,
lastname = @lastname
where empId = @id"
Dim cmdSQL As New SqlCommand(strSQL)
With cmdSQL.Parameters
.Add("@id", SqlDbType.Int).Value = empID.Text
.Add("@firstname", SqlDbType.NVarChar).Value = txt_firstname.Text
.Add("@lastname", SqlDbType.NVarChar).Value = txt_lsttname.Text
.Add("@tsizes", SqlDbType.Int).Value = txt_watersizes.Text
End With
MyrstPN(cmdSQL)
lblSuccess.ForeColor = Color.Green
lblSuccess.Text = "Records successfully updated"
End If
End Sub
不要尝试“管理”连接的打开/关闭。让 ASP.NET 为你做这些。有一个“自动”连接池,它将为你进行管理,而且不会成为性能瓶颈。在桌面应用程序中,在.NET之前,创建自己的连接对象并将其保持打开是一个非常好的主意。在.NET和ASP.NET中不需要这样做。
在ASP.NET中,请不要这样做!!
我们有一个便利的辅助例程:
Public Sub MyrstPN(cmdSQL As SqlCommand)
Using mycon As New SqlConnection(GetConstr)
Using (cmdSQL)
cmdSQL.Connection = mycon
mycon.Open()
cmdSQL.ExecuteNonQuery()
End Using
End Using
End Sub
不管是否有一些“更简洁”的清理代码,RB 选中的索引看起来是正确的。因此,我只能猜测这里可能有更多的活动部分,也许 RB 在某个更新面板之外,因此更改不会被看到。不要使用 AddWith
,它会使参数的数据类型变得不太清晰。
英文:
Well, your posted RB list has a different "id" then your code.
however, I would guess that you using perhaps a update panel, or some such.
And you don't show how that code snip is to be run (button click???).
Also, don't know where the conneciton object is comming from, but do NOT attempt to make some kind of persisted conneciton object (just don't go there!!).
So, code pattern like this:
Protected Sub Button1_Click(sender As Object, e As EventArgs)
If VetoRadioButtonList1.SelectedIndex = 0 Then
Dim strSQL As String =
"insert into WaterVolume (WaterSizes) values(@tsizes)"
Dim cmdSQL As New SqlCommand(strSQL)
cmdSQL.Parameters.Add("@tsizes", SqlDbType.Int).Value = txt_watersizes.Text
MyrstPN(cmdSQL)
lblSuccess.ForeColor = Color.Green
lblSuccess.Text = "Records successfully inserted"
Else
Dim strSQL As String =
"Update table2 set firstname = @firstname,
lastname = @lastname
where empId = @id"
Dim cmdSQL As New SqlCommand(strSQL)
With cmdSQL.Parameters
.Add("@id", SqlDbType.Int).Value = empID.Text
.Add("@firstname", SqlDbType.NVarChar).Value = txt_firstname.Text
.Add("@lastname", SqlDbType.NVarChar).Value = txt_lsttname.Text
.Add("@tsizes", SqlDbType.Int).Value = txt_watersizes.Text
End With
MyrstPN(cmdSQL)
lblSuccess.ForeColor = Color.Green
lblSuccess.Text = "Records successfully updated"
End If
End Sub
Do NOT attempt to "manage" the open/close of the connection. Let asp.net do this for you. There is a "automatic" connection pool, and it will be managed for you, and it not a performance bottle neck. In desktop land, and before .net, it was VERY good idea to create one's own connection object, and "keep" it open. this is not required in .net and asp.net
In asp.net, no, ,don't do that!!!
And our handy helper routine is this:
Public Sub MyrstPN(cmdSQL As SqlCommand)
Using mycon As New SqlConnection(GetConstr)
Using (cmdSQL)
cmdSQL.Connection = mycon
mycon.Open()
cmdSQL.ExecuteNonQuery()
End Using
End Using
End Sub
Regardless of some "shorter" cleaned up code?
That RB selected index looks to be correct. I can only thus guess that there are more moving parts here, and perhaps the RB is outside of some update panel, and thus the change is not being seen.
and don't use addwith - it leaves too much up in the air the data type of the parameter.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论