  | 
              
	              
	                
  
    |   | 
   
  
    
        
        [VB.NET]  รบกวนสอบถามเกี่ยวกับการเพิ่มข้อมูลใน DataGridView คะ (มือใหม่)     | 
   
  
    |   | 
   
 
 
 
	
		
			  | 
	   | 
	    | 
		
			  | 
	 
	
		
			  | 
		 		   | 
	  	    
          
            
			
	
			
			 
                เคยตั้งหัวข้อไว้แล้วคะ ตามนี้ 
https://www.thaicreate.com/dotnet/forum/115674.html 
 
 
แต่ตอนนี้มีปัญหาใหม่ ที่ต่อจากของเดิมก็คือ 
มันขึ้น error แบบนี้จะแก้ไขยังไงคะ 
 
**ต้องการให้แก้ไขข้อมูล/เพิ่มข้อมูล ใน datagrid ไปยัง database ที่มีอยู่ 
 
  
 
  
 
 
ส่วนอันนี้หน้า form ที่ออกแบบไว้คะ ตอนใส่ข้อมูล Id ไม่ขึ้นให้น่ะคะ ต้องทำยังไงคะ 
 
  
 
 
โค้ดทั้งหมดที่เขียนไว้คะ 
Code (VB.NET) 
Imports System.Data.SqlClient
Public Class VendorSpecific
    Dim objConn As New SqlConnection
    Dim objCmd As New SqlCommand
    Dim strConn, strSQL As String
    Dim da As New SqlDataAdapter
    Dim dt As New DataTable
    Private Sub VendorSpecific_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'SpecCompliantModule_01DataSet7.VendorSpecificMan' table. You can move, or remove it, as needed.
        Me.VendorSpecificManTableAdapter1.Fill(Me.SpecCompliantModule_01DataSet7.VendorSpecificMan)
        'TODO: This line of code loads data into the 'SpecCompliantModule_01DataSet9.VendorSpecificMan' table. You can move, or remove it, as needed.
        Me.VendorSpecificManTableAdapter.Fill(Me.SpecCompliantModule_01DataSet9.VendorSpecificMan)
        'TODO: This line of code loads data into the 'SpecCompliantModule_01DataSet1.VendorSpecific' table. You can move, or remove it, as needed.
        strConn = "Data Source=(LocalDB)\v11.0;Initial Catalog=SpecCompliantModule_01;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False"
        objConn.ConnectionString = strConn
        objConn.Open()
        strSQL = "SELECT * FROM [dbo].[VendorSpecificMan]"
        objCmd = New SqlCommand(strSQL, objConn)
        da.SelectCommand = objCmd
        da.Fill(dt)
        DataGridView1.DataSource = dt
        da.Update(dt)
        objConn.Close()
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        strConn = "Data Source=(LocalDB)\v11.0;Initial Catalog=SpecCompliantModule_01;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False"
        objConn.ConnectionString = strConn
        objConn.Open()
        strSQL = "INSERT INTO VendorSpecificMan([Vendor_Id],[Vendor_Feature Name],[Vendor_Sub Feature],[Vendor_Category Of Command],[Vendor_Command Name and Opcode]) VALUES (@[Vendor_Id],@[Vendor_Feature Name],@[Vendor_Sub Feature],@[Vendor_Category Of Command],@[Vendor_Command Name and Opcode])"
        For i As Integer = 0 To DataGridView1.RowCount - 1
            If Not DataGridView1.Rows(i).IsNewRow Then
                objCmd = New SqlCommand(strSQL, objConn)
                With objCmd
                    .Parameters.Add(New SqlParameter("@[Vendor_Id]", SqlDbType.Int).Value = DataGridView1.Rows(i).Cells(0).Value)
                    .Parameters.Add(New SqlParameter("@[Vendor_Feature Name]", SqlDbType.NVarChar).Value = DataGridView1.Rows(i).Cells(2).Value)
                    .Parameters.Add(New SqlParameter("@[Vendor_Sub Feature]", SqlDbType.NVarChar).Value = DataGridView1.Rows(i).Cells(3).Value)
                    .Parameters.Add(New SqlParameter("@[Vendor_Category Of Command]", SqlDbType.NVarChar).Value = DataGridView1.Rows(i).Cells(4).Value)
                    .Parameters.Add(New SqlParameter("@[Vendor_Command Name and Opcode]", SqlDbType.NVarChar).Value = DataGridView1.Rows(i).Cells(5).Value)
                End With
                objCmd.ExecuteNonQuery()
            End If
            MessageBox.Show("Data was saved success fully")
            objConn.Close()
        Next
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim hom As Form
        hom = HOME
        HOME.Show()
        Me.Hide()
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim specComp As Spec_complaint_module
        specComp = Spec_complaint_module
        Spec_complaint_module.Show()
        Me.Hide()
    End Sub
End Class
 
 
  Tag : .NET, Ms SQL Server 2008, Win (Windows App), VB.NET               
                        | 
           
          
            
		
  ประวัติการแก้ไข 2015-04-08 10:33:12 2015-04-08 10:34:51	
                             | 
           
          
            
              
                   | 
                   | 
                   | 
               
              
                   | 
                
                    
                      | Date :
                          2015-04-08 10:32:48 | 
                      By :
                          AOAOJS | 
                      View :
                          1034 | 
                      Reply :
                          4 | 
                     
                  | 
                   | 
               
              
                   | 
                   | 
                   | 
               
              | 
           
          
            | 
			 | 
           
         
	    
		             | 
		
			  | 
	 
	
		
			  | 
		  | 
		
			  | 
		
			  | 
	 
 
              
  
          
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 แล้วต้องประกาศตัวแปรยังไงคะ 
ให้ ID สามารถรันตัวเลขอัตโนมัติน่ะคะ 
เพราะตอนกรอกข้อมูลในส่วนของ Datagrid มันไม่สามารถแก้ไขช่อง ID ได้เลยคะ 
 
รบกวนด้วยนะคะ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2015-04-08 14:46:04 | 
                        By :
                            AOAOJS | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 มี error ใหม่คะ แบบนี้ 
 
  
 
 
                         
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2015-04-08 16:30:49 | 
                        By :
                            AOAOJS | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 สร้าง Vendor_Id เป็น IDENTITY ใน Table 
ศึกษาตามนี้น่ะ 
https://www.thaicreate.com/tutorial/sql-auto-increment-for-sql-server.html 
 
 
Code (VB.NET) 
                With objCmd
                '    .Parameters.Add(New SqlParameter("@[Vendor_Id]", SqlDbType.Int).Value = DataGridView1.Rows(i).Cells(0).Value)
                    .Parameters.Add(New SqlParameter("@[Vendor_Feature Name]", SqlDbType.NVarChar).Value = DataGridView1.Rows(i).Cells(2).Value)
                    .Parameters.Add(New SqlParameter("@[Vendor_Sub Feature]", SqlDbType.NVarChar).Value = DataGridView1.Rows(i).Cells(3).Value)
                    .Parameters.Add(New SqlParameter("@[Vendor_Category Of Command]", SqlDbType.NVarChar).Value = DataGridView1.Rows(i).Cells(4).Value)
                    .Parameters.Add(New SqlParameter("@[Vendor_Command Name and Opcode]", SqlDbType.NVarChar).Value = DataGridView1.Rows(i).Cells(5).Value)
                End With
                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2015-04-09 09:14:25 | 
                        By :
                            ipstarone | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
      		  
	
     | 
   
 
                 |