 |
|
ดู code นี้ให้ผมหน่อยครับ เกี่ยวกับปุ่ม Search ครับ มัน error Fill: SelectCommand.Connection property has not been initialized. |
|
 |
|
|
 |
 |
|
ขอดูสร้างสร้าง object conn หน่อยครับ
|
 |
 |
 |
 |
Date :
2010-08-28 19:48:22 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection conn;
SqlCommand comm;
string sqlstr;
private void frmsearch_Load(object sender, EventArgs e)
{
//ติดต่อฐานข้อมูล
string strconn = Class1.strconn;
conn = new SqlConnection();
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
conn.ConnectionString = strconn;
conn.Open();
//-----
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
sqlstr = "select * from FACULTY";
comm = new SqlCommand(sqlstr, conn);
da.SelectCommand = comm;
da.Fill(ds, "tmp");
comboBox1.DataSource = ds.Tables["tbm"];
comboBox1.DisplayMember = "FNAME";
comboBox1.ValueMember = "FNAME";
comboBox1.SelectedIndex = 0;
}
|
 |
 |
 |
 |
Date :
2010-08-28 20:02:13 |
By :
ปอ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
..................................
เดายากวุ้ย
เพราะประกาศ SqlConnection conn ไว้แล้ว
แต่ SelectCommand.Connection property has not been initialized. หมายความว่ายังได้กำหนด property
เป็นไปได้ไหมว่าเวลากดปุ่มแล้วกัน ไม่ได้ทำ event frmsearch_Load
object conn มันเลยยังไม่ได้ initialized
ถ้าต้องการแก้ก็คงของประกาศ instance ของ conn ใน button1_Click อีกครั้ง
ปล. เป็นวิธีแก้ที่ปลายเหตุนะ เขียนบ่อยๆ เดี๋ยวจะรู้เองว่าทำไมมันถึง error
|
 |
 |
 |
 |
Date :
2010-08-29 09:48:10 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับคุณ tungman
|
 |
 |
 |
 |
Date :
2010-08-29 10:33:31 |
By :
อิอิ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
รบกวนหน่อยค่ะ มัน Error ตัวอักษรสีแดงค่ะ ต้องแก้ไขยังไงค่ะ
Code (VB.NET)
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
|
 |
 |
 |
 |
Date :
2013-01-30 03:20:39 |
By :
แต้ว |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Conn ยังไม่ open
|
 |
 |
 |
 |
Date :
2013-01-30 07:53:26 |
By :
ป่านมา |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้ dataadapter ไม่ต้องเปิด conn จ้า
เพราะเป็น connection less
|
 |
 |
 |
 |
Date :
2013-01-30 15:27:29 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยังแก้ไม่ได้เลยค่ะ
|
 |
 |
 |
 |
Date :
2013-01-30 22:13:33 |
By :
แต้ว |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|