我刚开始制作这个项目,因为我是VB.net的新手。
更新部分在数据库中已有数据的地方起作用,但是当我按下保存按钮时,说IdNo
的数据还没有,插入部分就不能工作。已经打开
Public Sub AddBusinessInfo()
Dim dr As MySqlDataReader
Using conn As New MySqlConnection(connString)
Using cmd As New MySqlCommand("SELECT * FROM tblbusinessinfo WHERE IdNo=@IdNo", conn)
Try
conn.Open()
With cmd
.CommandType = CommandType.Text
.Parameters.AddWithValue("@IdNo", IDD)
End With
dr = cmd.ExecuteReader
With dr
.Read()
If (dr.HasRows) Then
Using cmd1 As New MySqlCommand("UPDATE tblbusinessinfo SET BusinessType=@BusinessType, BusinessName=@BusinessName, RegDate=@RegDate,
MotherCompany=@MotherCompany, TradeName=@TradeName, IPORegNo=@IPORegNo, Reg=@Reg, Province=@Province, District=@District,
CityMuni=@CityMuni, Brgy=@Brgy, ZipCode=@ZipCode, HBNo=HBNo, RFNo=@RFNo, BldgName=@BldgName, StName=@StName,
TelACode=@TelACode, TelNo=@TelNo, FaxACode=@FaxACode, FaxNo=@FaxNo, Email=@Email,
Mobile=@Mobile, SocialMedia=@SocialMedia WHERE IdNo=@IdNo", conn)
With cmd1
.CommandType = CommandType.Text
.Parameters.AddWithValue("@BusinessType", rbBTypeVal)
.Parameters.AddWithValue("@BusinessName", txtBName.Text)
.Parameters.AddWithValue("@RegDate", dtBusinessRegDate.Value)
.Parameters.AddWithValue("@MotherCompany", If(String.IsNullOrEmpty(txtMotherComp.Text), DBNull.Value, txtMotherComp.Text))
.Parameters.AddWithValue("@TradeName", If(String.IsNullOrEmpty(txtTradeName.Text), DBNull.Value, txtTradeName.Text))
.Parameters.AddWithValue("@IPORegNo", If(String.IsNullOrEmpty(txtIPOReg.Text), DBNull.Value, txtIPOReg.Text))
.Parameters.AddWithValue("@Reg", cmbReg.Text)
.Parameters.AddWithValue("@Province", cmbProvince.Text)
.Parameters.AddWithValue("@District", lblDistrict.Text)
.Parameters.AddWithValue("@CityMuni", cmbCityMuni.Text)
.Parameters.AddWithValue("@Brgy", cmbBrgy.Text)
.Parameters.AddWithValue("@ZipCode", lblZipCode.Text)
.Parameters.AddWithValue("@HBNo", If(String.IsNullOrEmpty(txtHBNo.Text), DBNull.Value, txtHBNo.Text))
.Parameters.AddWithValue("@RFNo", If(String.IsNullOrEmpty(txtRFNo.Text), DBNull.Value, txtRFNo.Text))
If (txtBuildingName.Text = Nothing) Then
.Parameters.AddWithValue("@BldgName", If(String.IsNullOrEmpty(txtBuildingName.Text), DBNull.Value, txtBuildingName.Text))
Else
.Parameters.AddWithValue("@BldgName", StrConv(txtBuildingName.Text, VbStrConv.ProperCase))
End If
If (txtStname.Text = Nothing) Then
.Parameters.AddWithValue("@StName", If(String.IsNullOrEmpty(txtStname.Text), DBNull.Value, txtStname.Text))
Else
.Parameters.AddWithValue("@StName", StrConv(txtStname.Text, VbStrConv.ProperCase))
End If
.Parameters.AddWithValue("@TelACode", If(String.IsNullOrEmpty(txtLAC.Text), DBNull.Value, txtLAC.Text))
.Parameters.AddWithValue("@TelNo", If(String.IsNullOrEmpty(txtLTN.Text), DBNull.Value, txtLTN.Text))
.Parameters.AddWithValue("@FaxACode", If(String.IsNullOrEmpty(txtFAC.Text), DBNull.Value, txtFAC.Text))
.Parameters.AddWithValue("@FaxNo", If(String.IsNullOrEmpty(txtFTN.Text), DBNull.Value, txtFTN.Text))
.Parameters.AddWithValue("@Email", If(String.IsNullOrEmpty(txtBusinessEmail.Text), DBNull.Value, txtBusinessEmail.Text))
.Parameters.AddWithValue("@Mobile", If(String.IsNullOrEmpty(txtMobile.Text), DBNull.Value, txtMobile.Text))
.Parameters.AddWithValue("@SocialMedia", If(String.IsNullOrEmpty(txtSocMedia.Text), DBNull.Value, txtSocMedia.Text))
.Parameters.AddWithValue("@IdNo", IDD)
End With
conn.Close()
If (txtBName.Text = "" Or cmbReg.Text = "" Or cmbProvince.Text = "" Or cmbCityMuni.Text = "" Or cmbBrgy.Text = "" Or lblDistrict.Text = "") Then
MsgBox("Fields with red texts are required!")
Else
Dim res As Integer = MsgBox("Are you sure you want to update the data?", MsgBoxStyle.YesNo, "System")
If (res = MsgBoxResult.Yes) Then
conn.Open()
cmd1.ExecuteNonQuery()
MsgBox("Success!")
End If
End If
End Using
conn.Close()
Else
Using cmd2 As New MySqlCommand("INSERT INTO tblbusinessinfo (IdNo, BusinessType, BusinessName, RegDate, MotherCompany, TradeName, IPORegNo, Reg,
Province, District, CityMuni, Brgy, ZipCode, HBNo, RFNo, BldgName, StName, TelACode, TelNo, FaxACode,
FaxNo, Email, Mobile, SocialMedia) VALUES (@IdNo, @BusinessType, @BusinessName, @RegDate, @MotherComp, @TradeName,
IPORegNo, @Reg, @Province, @District, @CityMuni, @Brgy, @ZipCode, @HBNo, @RFNo, @BldgName, @StName,
@TelACode, @TelNo, @FaxACode, @FaxNo, @Email, @Mobile, @SocialMedia)")
conn.Open()
With cmd2
.CommandType = CommandType.Text
.Parameters.AddWithValue("@IdNo", IDD)
.Parameters.AddWithValue("@BusinessType", rbBTypeVal)
.Parameters.AddWithValue("@BusinessName", txtBName.Text)
.Parameters.AddWithValue("@RegDate", dtBusinessRegDate.Value)
.Parameters.AddWithValue("@MotherComp", If(String.IsNullOrEmpty(txtMotherComp.Text), DBNull.Value, txtMotherComp.Text))
.Parameters.AddWithValue("@TradeName", If(String.IsNullOrEmpty(txtTradeName.Text), DBNull.Value, txtTradeName.Text))
.Parameters.AddWithValue("@IPORegNo", If(String.IsNullOrEmpty(txtIPOReg.Text), DBNull.Value, txtIPOReg.Text))
.Parameters.AddWithValue("@Reg", cmbReg.Text)
.Parameters.AddWithValue("@Province", cmbProvince.Text)
.Parameters.AddWithValue("@District", lblDistrict.Text)
.Parameters.AddWithValue("@CityMuni", cmbCityMuni.Text)
.Parameters.AddWithValue("@Brgy", cmbBrgy.Text)
.Parameters.AddWithValue("@ZipCode", lblZipCode.Text)
.Parameters.AddWithValue("@HBNo", If(String.IsNullOrEmpty(txtHBNo.Text), DBNull.Value, txtHBNo.Text))
.Parameters.AddWithValue("@RFNo", If(String.IsNullOrEmpty(txtRFNo.Text), DBNull.Value, txtRFNo.Text))
If (txtBuildingName.Text = Nothing) Then
.Parameters.AddWithValue("@BldgName", If(String.IsNullOrEmpty(txtBuildingName.Text), DBNull.Value, txtBuildingName.Text))
Else
.Parameters.AddWithValue("@BldgName", StrConv(txtBuildingName.Text, VbStrConv.ProperCase))
End If
If (txtStname.Text = Nothing) Then
.Parameters.AddWithValue("@StName", If(String.IsNullOrEmpty(txtStname.Text), DBNull.Value, txtStname.Text))
Else
.Parameters.AddWithValue("@StName", StrConv(txtStname.Text, VbStrConv.ProperCase))
End If
.Parameters.AddWithValue("@TelACode", If(String.IsNullOrEmpty(txtLAC.Text), DBNull.Value, txtLAC.Text))
.Parameters.AddWithValue("@TelNo", If(String.IsNullOrEmpty(txtLTN.Text), DBNull.Value, txtLTN.Text))
.Parameters.AddWithValue("@FaxACode", If(String.IsNullOrEmpty(txtFAC.Text), DBNull.Value, txtFAC.Text))
.Parameters.AddWithValue("@FaxNo", If(String.IsNullOrEmpty(txtFTN.Text), DBNull.Value, txtFTN.Text))
.Parameters.AddWithValue("@Email", If(String.IsNullOrEmpty(txtBusinessEmail.Text), DBNull.Value, txtBusinessEmail.Text))
.Parameters.AddWithValue("@Mobile", If(String.IsNullOrEmpty(txtMobile.Text), DBNull.Value, txtMobile.Text))
.Parameters.AddWithValue("@SocialMedia", If(String.IsNullOrEmpty(txtSocMedia.Text), DBNull.Value, txtSocMedia.Text))
End With
conn.Close()
If (txtBName.Text = "" Or cmbReg.Text = "" Or cmbProvince.Text = "" Or cmbCityMuni.Text = "" Or cmbBrgy.Text = "" Or lblDistrict.Text = "") Then
MsgBox("Fields with red texts are required!")
Else
Dim res As Integer = MsgBox("Are you sure you want to update the data?", MsgBoxStyle.YesNo, "System")
If (res = MsgBoxResult.Yes) Then
conn.Open()
cmd2.ExecuteNonQuery()
MsgBox("Success!")
End If
End If
End Using
End If
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Using
End Using
End Sub
已经解决了。 cmd2缺少参数,应该像这样:
Using cmd2 As New MySqlCommand("INSERT INTO tblbusinessinfo (IdNo, BusinessType, BusinessName, RegDate, MotherCompany, TradeName, IPORegNo, Reg,
Province, District, CityMuni, Brgy, ZipCode, HBNo, RFNo, BldgName, StName, TelACode, TelNo, FaxACode,
FaxNo, Email, Mobile, SocialMedia) VALUES (@IdNo, @BusinessType, @BusinessName, @RegDate, @MotherComp, @TradeName,
IPORegNo, @Reg, @Province, @District, @CityMuni, @Brgy, @ZipCode, @HBNo, @RFNo, @BldgName, @StName,
@TelACode, @TelNo, @FaxACode, @FaxNo, @Email, @Mobile, @SocialMedia)", conn)'
答案 0 :(得分:0)
这是因为您两次致电conn.Open()
:
首先在开头,紧随Try
关键字之后
第二次,如果If (dr.HasRows) Then
条件失败,它将进入Else
部分,在conn.Open()
语句之后,您在Using
处。
只需删除额外的Open
方法,您就可以了:)
答案 1 :(得分:0)
在cmd2声明中,您忘记了指定连接变量。
Using cmd2 As New MySqlCommand("INSERT INTO tblbusinessinfo (...)", ---> conn <---)
或单独指定:
cmd2.Connection = conn