HOME > .NET Framework > Forum > ดู code นี้ให้ผมหน่อยครับ เกี่ยวกับปุ่ม Search ครับ มัน error Fill: SelectCommand.Connection property has not been initialized.
ดู code นี้ให้ผมหน่อยครับ เกี่ยวกับปุ่ม Search ครับ มัน error Fill: SelectCommand.Connection property has not been initialized.
มัน Error ตรง da.Fill(ds, "tmp"); แล้วมันก็ขึ้นว่า Fill: SelectCommand.Connection property has not been initialized.
ช่วยดูให้ผมหน่อยครับกำลังหัดทำครับ
Code (C#)
private void button1_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
DataRow dr;
sqlstr = "select * from STUDENT where SID=@SID";
comm = new SqlCommand(sqlstr, conn);
comm.Parameters.Add("@SID", SqlDbType.Char).Value = textBox1.Text;
da.SelectCommand = comm;
da.Fill(ds, "tmp");
if (ds.Tables["tmp"].Rows.Count > 0)
{
dr = ds.Tables["tmp"].Rows[0];
textBox2.Text = dr["SID"].ToString();
textBox3.Text = dr["SNAME"].ToString();
textBox4.Text = dr["EMAIL"].ToString();
textBox5.Text = dr["MOBILE"].ToString();
textBox6.Text = dr["GPA"].ToString();
comboBox1.SelectedValue = dr["FNAME"].ToString();
}
else
{
MessageBox.Show("ไมพบข้อมูล");
}
}
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
If txtSearch.Text = "" Then Exit Sub
If IsNumeric(txtSearch.Text) = False Then
MessageBox.Show("กรุณาป้อนข้อมูลที่เป็นตัวเลขเท่านั้น !!!", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtSearch.Focus()
txtSearch.SelectAll()
Exit Sub
End If
Dim sqlSearch As String = ""
Dim tmpNetPaid As Single = 0
sqlSearch = "SELECT Agent.Agent_Name, Agent.Agent_Address,"
sqlSearch &= " Agent.ContactName, Agent.Agent_Tel,"
sqlSearch &= " Order.OrderID, Order.Order_Netpaid,"
sqlSearch &= " OrderDetail.EquipmentID, Equipment.Equip_name,"
sqlSearch &= " OrderDetail.Order_Num,BookOrder.Order_ReceivedDate,"
sqlSearch &= " Order.IsReceived,Order.IsNormal "
sqlSearch &= " FROM Equipment, OrderDetail, Agent, Order"
sqlSearch &= " WHERE (Equipment.EquipmentID = OrderDetail.EquipmentID)"
sqlSearch &= " AND (Equipment.AgentID = Agent.AgentID)"
sqlSearch &= " AND (OrderDetail.OrderID = Order.OrderID)"
sqlSearch &= " AND (Order.OrderID='" & txtSearch.Text & "')"
sqlSearch &= " AND (Order.IsReceived='0')"
sqlSearch &= " AND (Order.IsNormal='1')"
sqlSearch &= " ORDER BY OrderDetail.EquipmentID"
If IsFind = True Then
ds.Tables("Search").Clear()
End If
da = New SqlDataAdapter(sqlSearch, Conn)
da.Fill(ds, "Search")
If ds.Tables("Search").Rows.Count <> 0 Then
IsFind = True
lblAgentName.Text = CStr(ds.Tables("Search").Rows(0).Item("Agent_Name"))
lblAddress.Text = CStr(ds.Tables("Search").Rows(0).Item("Agent_Address"))
lblContactName.Text = CStr(ds.Tables("Search").Rows(0).Item("ContactName"))
lblTel.Text = CStr(ds.Tables("Search").Rows(0).Item("Agent_Tel"))
tmpNetPaid = CSng(ds.Tables("Search").Rows(0).Item("NetPaid"))
lblNetPaid.Text = tmpNetPaid.ToString("#,##0.00")
With grdEquipmentOrder
.AllowNavigation = False
.ReadOnly = True
.DataSource = ds.Tables("Search")
End With
Else
IsFind = False
ClearAllData()
grdEquipmentOrder.DataSource = Nothing
MessageBox.Show("หมายเลขใบสั่งซื้อที่คุณระบุไม่มี หรือได้รับอุปกรณ์ไปแล้ว !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtSearch.Focus()
txtSearch.SelectAll()
End If
FormatDataGridWithData()
End Sub