2011-02-01 92 views
0

我有一個DataGrid表tblpaymentview,我想從數據庫 我宣佈concstring 獲取值並編寫代碼爲查詢返回什麼

private void btnsearch_Click(object sender, EventArgs e) 
{ 
    try 
    { 
    DateTime time = dtpfromDate.Value; 
    String query = "Select jobcode,companyName,vehicleno,shipmentdate,totalamount,advance,drivername,fromPlace,destination from jobmastertable where (shipmentdate='"+time+"')"; 
    OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString); 
    DataSet ds = new DataSet(); 
    dAdapter.Fill(ds); 
    tblpaymentview.DataSource = ds.Tables["tblpaymentview"].DefaultView; 
    } 
    catch (Exception) 
    { 
    MessageBox.Show("The application had met with some errors please restart the application :\n error:closer MSAccess files"); 
    } 
} 

請誰能幫助我。我也試過(Datepicker)dtpfromdate.value.toshortdateString()也

+0

不是一個答案,但你真的應該換數據集,OleDbDataAdapter的,等在using語句 – Manatherin 2011-02-01 13:54:19

回答

0

你確定你沒有得到例外?

其次,你爲什麼假設該表格被稱爲tblpaymentview當它來自jobmastertable

0

我不知道這是否是您的問題,但ACCESS通常使用#代替日期格式。

+0

是日期/時間型 – sreenath 2011-02-01 13:39:29

+0

soory好友之後所有這些東西都添加了相同的異常發生IAM新到.net並且無法處理它謝謝ALL – sreenath 2011-02-01 13:51:10

0

在這種情況下,你可以使用OleDBParameters:

String query = "Select jobcode,companyName,vehicleno,shipmentdate,totalamount,advance,drivername,fromPlace,destination from jobmastertable where ([email protected])"; 
dAdapter.SelectCommand.Parameters.AddWithValue("@shipmentdate", time); 
+0

ya iam得到異常「標準表達式中的數據類型不匹配」。 – sreenath 2011-02-01 13:32:21

0

我想從你的表中查詢一些其他的條件,只是爲了讓你的「shipmentdate」欄目的真實數據類型的例子。這真的是約會嗎?但實際上是日期/時間字段。或作爲字符存儲的日期/時間。我會將其更改爲參數化查詢,並根據需要添加類型...如日期時間字段,而不是通過字符串連接。

0

嘗試的BindingSource如下:

BindingSource bindingsource1; 
DataSet ds; 
String query = "Select jobcode,companyName,vehicleno,shipmentdate,totalamount,advance,drivername,fromPlace,destination from jobmastertable where (shipmentdate='"+time+"')"; 
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query,Your Connection); 
ds = new DataSet(); 
dAdapter.Fill(ds); 
bindingsource1 = new BindingSource(); 
bindingsource1.DataSource = ds; 
bindingsource1.DataMember = ds.Tables[0].TableName; 
tblpaymentview.DataSource = bindingsource1; 

希望這個作品......