Dim dcid As DataColumn
Dim dcname As DataColumn
Dim dcaddress As DataColumn
Dim typecon As Type
Dim conname As Type
Dim conaddress As Type
dt = New DataTable
dcid = New DataColumn("ConId".ToString) ' ตรงนี้ผมจะกำหนดชนิดตัวแปรอย่างไรครับ
dt.Columns.Add(dcid)
dcname = New DataColumn("ConName".ToString)
dt.Columns.Add(dcname)
dcaddress = New DataColumn("ConAddress".ToString)
dt.Columns.Add(dcaddress)
Dim dr As DataRow
dr("ConId") = "C001" 'มันติดตรงนี้อครับ Object reference not set to an instance of an object.
dr("ConName") = "Nott"
dr("ConName") = "Nan"
dr("ConName") = "Nano"
dr("ConAddress") = "120/66 M10"
dr("ConAddress") = "143/294 M11"
dt.Rows.Add(dr)
dgshow.DataSource = dt
คือจริงๆแล้ว ผมตั้งใจว่าจะประกาศตัวแปรข้างบนเป็นแบบนี้อะครับ
Dim typecon As String
Dim conname As String
Dim conaddress As String
แล้วก็จะให้ตรง dcid DataColumn เป็นแบบนี้
dcid = New DataColumn("ConId",typecon)
แต่พอทำแบบนี้แล้วมันก็ขึ้น Error1Value of type 'String' cannot be converted to 'System.Type'.
ก็เลยรองแก้มาเป็นแบบนี้
Dim typecon As type
Dim conname As type
Dim conaddress As type
dcid = New DataColumn("ConId",typecon)
แต่พอ run แล้วก็ติด Error นี้อีก 'dataType' argument cannot be null. Parameter name: dataType
ผมก็เลยแก้ให้เป็น
dcid = New DataColumn("ConId".ToString)
แบบที่เห็นกันด้านบน แต่ว่าก็ยังติดอีก จนปัญหา ช่วยหน่อยครับ อธิบายด้วยก็ดีครับ
String[] _DayOfWeek = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
DataTable Dt = new DataTable();
//add columns to datatable
Dt.Columns.Add(new DataColumn("ID", Type.GetType("System.Int16")));
Dt.Columns.Add(new DataColumn("DayOfWeek", Type.GetType("System.String")));
//add rows to datatable
for (int i = 0; i < 7; i++)
{
DataRow Dr = Dt.NewRow();
Dr["ID"] = i + 1;
Dr["DayOfWeek"] = _DayOfWeek[i];
Dt.Rows.Add(Dr);
}
การสร้างข้อมูลให้ DataTable (VB.NET)
Dim _DayOfWeek() As String = New String() { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }
Dim Dt As DataTable = New DataTable()
'add columns to datatable
Dim Column1 As DataColumn = New DataColumn()
Column1.ColumnName = "ID"
Column1.DataType = Type.GetType("System.Int16")
Dim Column2 As DataColumn = New DataColumn()
Column2.ColumnName = "DayOfWeek"
Column2.DataType = Type.GetType("System.String")
Dt.Columns.Add(Column1)
Dt.Columns.Add(Column2)
'add rows to datatable
Dim i As Integer
For i = 0 To 6
Dim Dr As DataRow = Dt.NewRow();
Dr("ID") = i + 1;
Dr("DayOfWeek") = _DayOfWeek(i);
Dt.Rows.Add(Dr);
Next i
String[] _DayOfWeek = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
DataTable Dt = new DataTable();
Dt.TableName = "Mytable"; //เพิ่มบรรทัดนี้
//add columns to datatable
Dt.Columns.Add(new DataColumn("ID", Type.GetType("System.Int16")));
Dt.Columns.Add(new DataColumn("DayOfWeek", Type.GetType("System.String")));
//add rows to datatable
for (int i = 0; i < 7; i++)
{
DataRow Dr = Dt.NewRow();
Dr["ID"] = i + 1;
Dr["DayOfWeek"] = _DayOfWeek[i];
Dt.Rows.Add(Dr);
}
DataSet Ds = new DataSet(); //เพิ่มบรรทัดนี้
Ds.Tables.Add(Dt); //เพิ่มบรรทัดนี้
Code (VB.NET)
Dim _DayOfWeek() As String = New String() { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }
Dim Dt As DataTable = New DataTable()
Dt.TableName = "Mytable" 'เพิ่มบรรทัดนี้
'add columns to datatable
Dim Column1 As DataColumn = New DataColumn()
Column1.ColumnName = "ID"
Column1.DataType = Type.GetType("System.Int16")
Dim Column2 As DataColumn = New DataColumn()
Column2.ColumnName = "DayOfWeek"
Column2.DataType = Type.GetType("System.String")
Dt.Columns.Add(Column1)
Dt.Columns.Add(Column2)
'add rows to datatable
Dim i As Integer
For i = 0 To 6
Dim Dr As DataRow = Dt.NewRow();
Dr("ID") = i + 1;
Dr("DayOfWeek") = _DayOfWeek(i);
Dt.Rows.Add(Dr);
Next i
Dim Ds As DataSet = New DataSet() 'เพิ่มบรรทัดนี้
Ds.Tables.Add(Dt); 'เพิ่มบรรทัดนี้