Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > Forum > รบกวนช่วยดูคำสั่ง command.ExecuteNonQuery() มันเอ่อเร่อ



 

รบกวนช่วยดูคำสั่ง command.ExecuteNonQuery() มันเอ่อเร่อ

 



Topic : 112164



โพสกระทู้ ( 5 )
บทความ ( 0 )



สถานะออฟไลน์




Code
Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSave.Click sql = "SELECT COUNT(*) FROM Paymentt WHERE " & _ "(RoomNo = @r) AND (Month = @m) AND (Year = @y)" AddParameters(sql) Dim count As Integer = command.ExecuteScalar() If count = 0 Then sql = "INSERT INTO Paymentt(" & _ "RoomNo, Month, Year, BeforWaterSupply, AfterWaterSupply, TotalUnitWater, WaterSupply,BeforElectricity, AfterElectricity,TotalUnitElectricity,Electricity,Rental,Clean,ToTal,FireUint,WaterUnit,BeginFire,BeginWater)" & _ "VALUES(@room, @month, @year, @BeforW, @AfterWater, @UnitWater, @Water, @BeforElectric, @AfterElectric, @UnitElectric, @Electric, @rent, @cleans, @totals,@UFire,@UWater,@BeginF,@BeginW)" Else sql = "UPDATE Paymentt SET " & _ "RoomNo = @room, Month = @month, Year = @year, BeforWaterSupply = @BeforW,AfterWaterSupply = @AfterWater,TotalUnitWater = @UnitWater, FireUin t= @UFire, WaterUnit = @UWater, BeginFire = @BeginF, BeginWater = @BeginW" & _ "WaterSupply = @Water, BeforElectricity = @BeforElectric, AfterElectricity = @AfterElectric, TotalUnitElectricity = @UnitElectric, Electricity = @Electric, Rental = @rent, Clean = @cleans, ToTal = @totals " & _ "WHERE (RoomNo = @r) AND (Month = @m) AND (Year = @y)" End If command.Parameters.Clear() command.CommandText = sql command.Parameters.AddWithValue("room", ComboRoom.SelectedItem) command.Parameters.AddWithValue("month", ComboMonth.SelectedItem) command.Parameters.AddWithValue("year", ComboYear.SelectedItem) command.Parameters.AddWithValue("BeforW", Textbeforf.Text) command.Parameters.AddWithValue("AfterWater", Textafterf.Text) command.Parameters.AddWithValue("UnitWater", Textfunit.Text) command.Parameters.AddWithValue("Water", TextWater.Text) command.Parameters.AddWithValue("BeforElectric", TextOthers.Text) command.Parameters.AddWithValue("AfterElectric", Textbeforw.Text) command.Parameters.AddWithValue("UnitElectric", Textwunit.Text) command.Parameters.AddWithValue("Electric", TextElectric.Text) command.Parameters.AddWithValue("rent", TextRental.Text) command.Parameters.AddWithValue("cleans", TextOthers.Text) command.Parameters.AddWithValue("totals", TextTotal.Text) command.Parameters.AddWithValue("UFire", Textunitwater.Text) command.Parameters.AddWithValue("UWater", Textunitfire.Text) command.Parameters.AddWithValue("BeginF", TextbeginFireUint.Text) command.Parameters.AddWithValue("BeginW", TextbeginWaterUnit.Text) If count > 0 Then command.Parameters.AddWithValue("r", ComboRoom.SelectedItem) command.Parameters.AddWithValue("m", ComboMonth.SelectedItem) command.Parameters.AddWithValue("y", ComboYear.SelectedItem) End If Dim r As Integer = command.ExecuteNonQuery() << รองรันดูแล้วมันขึ้นเอ่อเร่อตรงนี้ ต้องแก้ยังไงเหรอค่ะรบกวนช่วยบอกที -/\- ขอบคุณค่ะ If r > 0 Then MessageBox.Show("บันทึกข้อมูลแล้ว") Else MessageBox.Show("เกิดข้อผิดพลาดในการบันทึกข้อมูล") End If End Sub




Tag : .NET, Win (Windows App), VB.NET







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2014-10-26 20:31:49 By : kkan View : 3263 Reply : 12
 

 

No. 1



โพสกระทู้ ( 296 )
บทความ ( 0 )



สถานะออฟไลน์
Facebook

Error อะไรก็ไม่บอก






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-10-27 07:20:57 By : zarooman
 


 

No. 2



โพสกระทู้ ( 9,556 )
บทความ ( 2 )



สถานะออฟไลน์


มันควรประกาศตัวแปร sql ก่อนไหมครับ

dim sql as String;

Dim count As Integer = command.ExecuteScalar() บันทัดนี้
ควรมีคำสั่งอะไรมาทำก่อน หรือเปล่าครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-10-27 08:19:09 By : Chaidhanan
 

 

No. 3



โพสกระทู้ ( 174 )
บทความ ( 0 )



สถานะออฟไลน์


ตอบความคิดเห็นที่ : 2 เขียนโดย : Chaidhanan เมื่อวันที่ 2014-10-27 08:19:09
รายละเอียดของการตอบ ::
ตามพี่ Chaidhanan บอกเลยครับ โค๊ด ที่ประกาศไว้ก่อนหน้านี้มีไหมครับ

แล้ว Add Parameter ไมไม่มี @ข้างหน้าละครับ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-10-27 11:25:50 By : nongpaoza
 


 

No. 4



โพสกระทู้ ( 5 )
บทความ ( 0 )



สถานะออฟไลน์


ตอนกดปุ่มบันทึกมันขึ้นไดอะล็อกบ๊อกซ์นี้
SqlException was unhandled
implict conversion from data type nvarchar to varbinary is not allowed. use the conver function to run this query.


คือต้องการให้ตรวจสอบว่าเคยได้บันทึกข้อมูลในเดือนที่เลือกหรือยัง ถ้ายังไม่มีข้อมูลให้บันทึกข้อมูลในตาราง แต่ถ้ามีข้อมูลอยู่แล้วให้อ่านค่าแล้วอัพเดทข้อมูลเดิม ประมาณนี้นะค่ะ

Add Parameter ถ้าใส่ @ มันขึ้นSyntax error ค่ะ

(เพิ่งเริ่มศึกษาแล้วทำตามหนังสือดู แต่ทำแล้วมันขึ้นเอ่อเร่อ)


ประวัติการแก้ไข
2014-10-28 14:21:05
2014-10-28 14:25:03
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-10-28 14:18:50 By : kkan
 


 

No. 5

Guest


.php
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-10-28 14:28:44 By : ห้ามตอบเกินวันละ 2 กระทู้
 


 

No. 6



โพสกระทู้ ( 154 )
บทความ ( 0 )



สถานะออฟไลน์


ตั้งแต่บรรทัด

command.Parameters.AddWithValue("room", ComboRoom.SelectedItem)

ไล่มาเลยพวก AddWithValue ให้ใส่ @ ด้วยทุกบรรทัดเลย

command.Parameters.AddWithValue("@room", ComboRoom.SelectedItem)
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-10-28 14:49:58 By : gunnermontana
 


 

No. 7



โพสกระทู้ ( 5 )
บทความ ( 0 )



สถานะออฟไลน์


ตอบความคิดเห็นที่ : 6 เขียนโดย : gunnermontana เมื่อวันที่ 2014-10-28 14:49:58
รายละเอียดของการตอบ ::
ลองใส่แล้ว แต่ก็เป็นเหมือนเดิมค่ะ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-10-28 15:09:02 By : kkan
 


 

No. 8

Guest


Code (VB.NET)
Private Function IsExist(ByVal room As System.String, ByVal month As System.String, ByVal year As System.String) As Boolean
   Dim connection ....
   Dim command ....

   connection.Open()
   Boolean result = If(((command.ExecuteScalar() As Integer) > 0), True, False)
   connection.Close()

   Return result
End Function

Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSave.Click
   If Not IsExist(ComboRoom.SelectedItem, ComboMonth.SelectedItem, ComboYear.SelectedItem) Then
      Insert()
   Else
      Update()
   End If
End Sub


แต่จริงๆ มันแหม่งๆ ตรง AddParameters(sql) นะ

ทำไมโผล่มาอีกทีเป็น ExecuteScalar เฉยเลย
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-10-28 15:53:28 By : ห้ามตอบเกินวันละ 2 กระทู้
 


 

No. 9



โพสกระทู้ ( 4,436 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

http://jwcooney.com/2012/09/10/asp-net-choosing-parameters-add-or-parameters-addwithvalue/

Dim aDateVariable As String = "1/2/2012"
Dim commandObject As New SqlCommand()
commandObject.Connection = conn
commandObject.CommandType = CommandType.StoredProcedure
commandObject.Parameters.AddWithValue("@aDateParameter","1/2/2012" )

มันต้องเซ็ต CommandType ใหม่ป่ะครับ(อันนี้ไม่รู้) ไม้งั้นก็น่าจะเป็นที่ SQL
ไม่ลองทำแยกดูระหว่าง INSERT กะ UPDATE จะได้ตัดปัญหาเรื่อง SQL ได้
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-10-28 16:06:32 By : lamaka.tor
 


 

No. 10



โพสกระทู้ ( 5 )
บทความ ( 0 )



สถานะออฟไลน์


ตอบความคิดเห็นที่ : 8 เขียนโดย : ห้ามตอบเกินวันละ 2 กระทู้ เมื่อวันที่ 2014-10-28 15:53:28
รายละเอียดของการตอบ ::
ลองทำตามหนังสือดูนะค่ะ แต่มันรันไม่ได้ - -'

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-10-28 20:08:11 By : kkan
 


 

No. 11

Guest


นายทำแบบนี้แล้วกัน

1. ตรวจสอบสอบข้อมูลว่ามีอยู่แล้วหรือไม่ (is exist)

2. ถ้ามี -> update

3. ถ้าไม่มี -> insert

ส่วนการติดต่อ database หลักๆ เลยก็คือ

1. สร้าง connection

2. สร้าง command

3. execute command

Code (VB.NET)
Imports System.Data.SqlClient
Imports System.Text

Public Class Form1
    Private connectionString As String

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.connectionString = "ใส่ connection string เอง"
    End Sub

    Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
        If Me.IsExist() Then
            Me.UpdateItem()
        Else
            Me.InsertItem()
        End If
    End Sub

    Private Function IsExist() As Boolean
        Dim result As Boolean

        Using connection As New SqlConnection(connectionString)
            Using command = connection.CreateCommand()
                Dim commandString As New StringBuilder
                commandString.AppendLine("SELECT COUNT(*)")
                commandString.AppendLine("FROM Paymentt")
                commandString.AppendLine("WHERE (RoomNo = @Room) AND (Month = @Month) AND (Year = @Year);")

                command.CommandTimeout = 240
                command.CommandType = CommandType.Text
                command.CommandText = commandString.ToString()
                command.Parameters.AddWithValue("@Room", ComboRoom.SelectedItem)
                command.Parameters.AddWithValue("@Month", ComboMonth.SelectedItem)
                command.Parameters.AddWithValue("@Year", ComboYear.SelectedItem)

                Try
                    connection.Open()
                    result = If((DirectCast(command.ExecuteScalar(), Integer) > 0), True, False)
                    connection.Close()
                Catch ex As Exception
                    Me.HandlerException(ex)
                    result = False
                End Try
            End Using
        End Using

        Return result
    End Function

    Private Sub InsertItem()
        Dim rowAffect As Integer : rowAffect = 0

        Using connection As New SqlConnection(connectionString)
            Using command = connection.CreateCommand()
                Dim commandString As New StringBuilder
                commandString.AppendLine("INSERT INTO Paymentt")
                commandString.AppendLine("(")
                commandString.AppendLine("RoomNo,")
                commandString.AppendLine("Month,")
                commandString.AppendLine("Year,")
                commandString.AppendLine("BeforWaterSupply,")
                commandString.AppendLine("AfterWaterSupply,")
                commandString.AppendLine("TotalUnitWater,")
                commandString.AppendLine("WaterSupply,")
                commandString.AppendLine("BeforElectricity,")
                commandString.AppendLine("AfterElectricity,")
                commandString.AppendLine("TotalUnitElectricity,")
                commandString.AppendLine("Electricity,")
                commandString.AppendLine("Rental,")
                commandString.AppendLine("Clean,")
                commandString.AppendLine("ToTal,")
                commandString.AppendLine("FireUint,")
                commandString.AppendLine("WaterUnit,")
                commandString.AppendLine("BeginFire,")
                commandString.AppendLine("BeginWater")
                commandString.AppendLine(")")
                commandString.AppendLine("VALUES")
                commandString.AppendLine("(")
                commandString.AppendLine("@RoomNo,")
                commandString.AppendLine("@Month,")
                commandString.AppendLine("@Year,")
                commandString.AppendLine("@BeforWaterSupply,")
                commandString.AppendLine("@AfterWaterSupply,")
                commandString.AppendLine("@TotalUnitWater,")
                commandString.AppendLine("@WaterSupply,")
                commandString.AppendLine("@BeforElectricity,")
                commandString.AppendLine("@AfterElectricity,")
                commandString.AppendLine("@TotalUnitElectricity,")
                commandString.AppendLine("@Electricity,")
                commandString.AppendLine("@Rental,")
                commandString.AppendLine("@Clean,")
                commandString.AppendLine("@ToTal,")
                commandString.AppendLine("@FireUint,")
                commandString.AppendLine("@WaterUnit,")
                commandString.AppendLine("@BeginFire,")
                commandString.AppendLine("@BeginWater")
                commandString.AppendLine(")")

                command.CommandTimeout = 30
                command.CommandType = CommandType.Text
                command.CommandText = commandString.ToString()
                command.Parameters.AddWithValue("@Room", ComboRoom.SelectedItem)
                command.Parameters.AddWithValue("@Month", ComboMonth.SelectedItem)
                command.Parameters.AddWithValue("@Year", ComboYear.SelectedItem)
                command.Parameters.AddWithValue("@BeforW", Textbeforf.Text)
                command.Parameters.AddWithValue("@AfterWater", Textafterf.Text)
                command.Parameters.AddWithValue("@UnitWater", Textfunit.Text)
                command.Parameters.AddWithValue("@Water", TextWater.Text)
                command.Parameters.AddWithValue("@BeforElectric", TextOthers.Text)
                command.Parameters.AddWithValue("@AfterElectric", Textbeforw.Text)
                command.Parameters.AddWithValue("@UnitElectric", Textwunit.Text)
                command.Parameters.AddWithValue("@Electric", TextElectric.Text)
                command.Parameters.AddWithValue("@rent", TextRental.Text)
                command.Parameters.AddWithValue("@cleans", TextOthers.Text)
                command.Parameters.AddWithValue("@totals", TextTotal.Text)
                command.Parameters.AddWithValue("@UFire", Textunitwater.Text)
                command.Parameters.AddWithValue("@UWater", Textunitfire.Text)
                command.Parameters.AddWithValue("@BeginF", TextbeginFireUint.Text)
                command.Parameters.AddWithValue("@BeginW", TextbeginWaterUnit.Text)

                Try
                    connection.Open()
                    rowAffect = command.ExecuteNonQuery()
                    connection.Close()

                    Me.ExecuteSuccess("Insert complete", rowAffect)
                Catch ex As Exception
                    Me.HandlerException(ex)
                End Try
            End Using
        End Using
    End Sub

    Private Sub UpdateItem()
        Dim rowAffect As Integer : rowAffect = 0

        Using connection As New SqlConnection(connectionString)
            Using command = connection.CreateCommand()
                Dim commandString As New StringBuilder
                commandString.AppendLine("UPDATE Paymentt")
                commandString.AppendLine("SET")
                commandString.AppendLine("BeforWaterSupply = @BeforW,")
                commandString.AppendLine("AfterWaterSupply = @AfterWater,")
                commandString.AppendLine("TotalUnitWater = @UnitWater,")
                commandString.AppendLine("FireUint= @UFire,")
                commandString.AppendLine("WaterUnit = @UWater,")
                commandString.AppendLine("BeginFire = @BeginF,")
                commandString.AppendLine("BeginWater = @BeginW,")
                commandString.AppendLine("WaterSupply = @Water,")
                commandString.AppendLine("BeforElectricity = @BeforElectric,")
                commandString.AppendLine("AfterElectricity = @AfterElectric,")
                commandString.AppendLine("TotalUnitElectricity = @UnitElectric,")
                commandString.AppendLine("Electricity = @Electric,")
                commandString.AppendLine("Rental = @rent,")
                commandString.AppendLine("Clean = @cleans,")
                commandString.AppendLine("ToTal = @totals")
                commandString.AppendLine("WHERE")
                commandString.AppendLine("(RoomNo = @Room) AND (Month = @Month) AND (Year = @Year)")

                command.CommandTimeout = 30
                command.CommandType = CommandType.Text
                command.CommandText = commandString.ToString()
                command.Parameters.AddWithValue("@BeforW", Textbeforf.Text)
                command.Parameters.AddWithValue("@AfterWater", Textafterf.Text)
                command.Parameters.AddWithValue("@UnitWater", Textfunit.Text)
                command.Parameters.AddWithValue("@Water", TextWater.Text)
                command.Parameters.AddWithValue("@BeforElectric", TextOthers.Text)
                command.Parameters.AddWithValue("@AfterElectric", Textbeforw.Text)
                command.Parameters.AddWithValue("@UnitElectric", Textwunit.Text)
                command.Parameters.AddWithValue("@Electric", TextElectric.Text)
                command.Parameters.AddWithValue("@rent", TextRental.Text)
                command.Parameters.AddWithValue("@cleans", TextOthers.Text)
                command.Parameters.AddWithValue("@totals", TextTotal.Text)
                command.Parameters.AddWithValue("@UFire", Textunitwater.Text)
                command.Parameters.AddWithValue("@UWater", Textunitfire.Text)
                command.Parameters.AddWithValue("@BeginF", TextbeginFireUint.Text)
                command.Parameters.AddWithValue("@BeginW", TextbeginWaterUnit.Text)

                command.Parameters.AddWithValue("@Room", ComboRoom.SelectedItem)
                command.Parameters.AddWithValue("@Month", ComboMonth.SelectedItem)
                command.Parameters.AddWithValue("@Year", ComboYear.SelectedItem)

                Try
                    connection.Open()
                    rowAffect = command.ExecuteNonQuery()
                    connection.Close()

                    Me.ExecuteSuccess("Update complete", rowAffect)
                Catch ex As Exception
                    Me.HandlerException(ex)
                End Try
            End Using
        End Using
    End Sub

    Private Sub ExecuteSuccess(ByVal Message As String, ByVal RowAffect As Integer)
        MessageBox.Show(String.Format("{0}: {1} records.", Message, RowAffect.ToString("#,##0")), "บันทึกข้อมูลแล้ว")
    End Sub

    Private Sub HandlerException(ByVal ex As Exception)
        MessageBox.Show(ex.Message, "เกิดข้อผิดพลาดในการบันทึกข้อมูล")
    End Sub
End Class

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-10-29 08:48:20 By : ห้ามตอบเกินวันละ 2 กระทู้
 


 

No. 12



โพสกระทู้ ( 5 )
บทความ ( 0 )



สถานะออฟไลน์


ตอบความคิดเห็นที่ : 11 เขียนโดย : ห้ามตอบเกินวันละ 2 กระทู้ เมื่อวันที่ 2014-10-29 08:48:20
รายละเอียดของการตอบ ::
ขอบคุณค่ะ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-11-02 18:15:58 By : kkan
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : รบกวนช่วยดูคำสั่ง command.ExecuteNonQuery() มันเอ่อเร่อ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 00
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่