วันพฤหัสบดีที่ 26 มีนาคม พ.ศ. 2552

Index was out of range. Must be non-negative and less than the size of the collection

error msg : Index was out of range. Must be non-negative and less than the size of the collection.

ปัญหา : ขณะที่ผูก datasource เข้ากับ datagridview จะมีเรียกอีเวนต์ _RowEnter ทำงานอัตโนมัติ แต่เรายังไม่ได้คลิกเลือกใน datagridview หรือไม่เกิดการ SelectedRows() ดังนั้นค่า index ที่ใน SelectedRow(0) ที่เรากำหนดในอีเวนต์ _RowEnter จึงอ้างอิงผิดพลาด

สาเหตุ : เกิดภายใต้อีเวนต์ DataGridView_RowEnter ซึ่งเราได้กำหนดให้มีการทำงานดังนี้
***SelectedRow(0) <--- ขณะที่เรายังไม่ได้เลือกจริงๆ ใน datagridview
-----------------------------------------------------------------------------------------
Private Sub DataGridView_RowEnter(....)
txtPileID.Text = dgvPile.SelectedRows(0).Cells("colCode").Value.ToString
End Sub
-----------------------------------------------------------------------------------------
แก้ไข : ภายในอีเวนต์ DataGridView_RowEnter ให้ตรวจสอบก่อนว่าได้เกิดการเลือก Row ใน DataGridView หรือยัง??
-----------------------------------------------------------------------------------------
Private Sub DataGridView_RowEnter(....)
If Not
DataGridView.SelectedRows.Count = 0 Then
txtPileID.Text = dgvPile.SelectedRows(0).Cells("colCode").Value.ToString
Else
Exit Sub
End If
End Sub
-----------------------------------------------------------------------------------------


วันพฤหัสบดีที่ 19 มีนาคม พ.ศ. 2552

Date Time without time in SQL

เมื่อต้องการเฉพาะวันที่แต่ไม่เอาเวลามาด้วย :

Select Convert (varchar(12),getdate(),101) --> 03/19/2009

Select Convert (varchar(12),getdate(),1) --> 03/19/09

The 101 means “mm/dd/yyyy” format, but there are a bunch of other codes you can use. 108 will return just the time “hh:mm:ss” for instance.


วันพุธที่ 18 มีนาคม พ.ศ. 2552

Get Table Names in SQL

Use the following for user tables
----------------------------------------------
SELECT name
FROM dbo.sysobjects
WHERE xtype = 'U'
ORDER BY dbo.sysobjects.name
----------------------------------------------
some other types are:
'V' - views
'S' - system tables


วันอังคารที่ 17 มีนาคม พ.ศ. 2552

To reset auto increment

ก่อนอื่นต้องลบข้อมูลในตารางก่อนนะครับแล้วรันคำสั่งนี้....

DBCC CHECKIDENT
('table_name', RESEED, 0) ;
GO

ใส่ชื่อตารางตรง
table_name


วันพุธที่ 4 มีนาคม พ.ศ. 2552

Add data to datagridview

Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
Dim RowArray() As String = _
{txtCode.Text, txtName.Text, "", txtQty.Text}
DataGridView1.Rows.Add(RowArray)
End Sub


Get column names from MSSQL

-------------------------------------------------------------------------------------------------------
มันจะดึงมาทั้งดาต้าเบสเลยคับ ผมก็เอาไปปะใน Excel อีกทีแล้วค่อยกรองเอาตารางที่ต้องการแสดงผล...

SELECT table_name=sysobjects.name,
column_name=syscolumns.name,
datatype=systypes.name,
length=syscolumns.length
FROM sysobjects
JOIN syscolumns ON sysobjects.id = syscolumns.id
JOIN systypes ON syscolumns.xtype=systypes.xtype
WHERE sysobjects.xtype='U'
ORDER BY sysobjects.name,syscolumns.colid
-------------------------------------------------------------------------------------------------------
ถ้าต้องการแค่ table ใดๆ 'table_name' ใส่ชื่อตารางที่ต้องการได้เลย

SELECT * FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'table_name'
-------------------------------------------------------------------------------------------------------







วันอังคารที่ 3 กุมภาพันธ์ พ.ศ. 2552

Login Form VB.NET 01

Dim MyDS As New DataSet
Dim conns As String = "Data Source=ISAAC;Initial Catalog=SETH;Integrated Security=True"
Dim Conn As New SqlConnection
Conn.ConnectionString = conns

Dim Cmd As SqlDataAdapter
Cmd = New SqlDataAdapter("select * from login where username='" & Trim(C1TextBox1.Text) & "' and password='" & TRIM(C1TextBox2.Text)& "'", Conn)

Cmd.Fill(MyDS, "login")
Dim table As DataTable = MyDS.Tables("login")
Dim i As Integer
Dim j As Integer
if ( table.Rows.Count= 1)
Form1
.Show()
Me.Close()

Else
MsgBox("Incorrect Login")
C1TextBox1
.Focus()
End If

-----------------------------------------------------------------------------------------------------
As a side note:
You sql statement is susceptible to SQL
injection. Unless you are doing some sort of sanitizing on your textbox
input, you should consider using
parameters in your queries.

For instance, if I were to type
x' or 1 = 1; --
in your textbox, I have just bypassed your login procedure (because 1 = 1 will always evaluate true and thus return all rows).
You should use command parameters as demonstrated in the snippet.
-----------------------------------------------------------------------------------------------------
Dim adapter As System.Data.SqlClient.SqlDataAdapter
Dim cmd As New System.Data.SqlClient.SqlCommand("Select * from login where username = @user and password = @pass")

cmd
.Parameters.Add(New System.Data.SqlClient.SqlParameter("@user", ClTextBox1.Text.Trim()))
cmd
.Parameters.Add(New System.Data.SqlClient.SqlParameter("@pass", ClTextBox2.Text.Trim()))

adapter
= New System.Data.SqlClient.SqlDataAdapter(cmd)
-----------------------------------------------------------------------------------------------------