PDA

View Full Version : جستجو در دیتابیس و نمایش در دیتاگرید



toopak
شنبه 26 مرداد 1387, 15:16 عصر
خسته نباشید
آقا یه سوال داشتم از خدمتتون
من یه سرچ نوشتم که اطلاعات مطابق با جستجو رو توی datagrid نمایش بده
کدش اینه


Dim ds As New DataSet
Dim inssql As String
Dim cmdsql As New OleDbCommand

If rname.Checked = True Then
inssql = "SELECT * FROM tbaccounts WHERE accname='" & txtname.Text & "' " '& " txtname.Text &'"
ElseIf rtell.Checked = True Then
inssql = "SELECT * FROM tbaccounts WHERE acctell= " & txttell.Text
ElseIf rid.Checked = True Then
inssql = "SELECT * FROM tbaccounts WHERE accname= " & txtname.Text
End If

con.Open()
cmdsql.Connection = con
cmdsql.CommandText = inssql
Dim da As New OleDbDataAdapter(inssql, con)


da.Fill(ds, "tbaccounts")
DataGridView1.DataBindings.Add(New Binding("datasource", ds, "tbaccounts"))
DataGridView1.Visible = True



con.Close()


برای اولین بار درست کار میکنه اما بار دوم که دکمه جستجو رو میزنم
دیباگر برنامه برای اون قسمت که تو کد قرمز کردم
این ارور رو میده

This causes two bindings in the collection to bind to the same property.
Parameter name: binding

مشکل کجاس؟:متفکر:
راه حل چیه؟
ممنون:خجالت:

ALI TT
یک شنبه 27 مرداد 1387, 01:48 صبح
چرا دیتا ست رو قبل از استفاده Clear نمی کنی ، ممکنه مشکل از اینجا باشه ؟
Clear کردن رو قبل از اینکه Fill کنی باید بکار ببری .


Dim ds As New DataSet
Dim inssql As String
Dim cmdsql As New OleDbCommand

If rname.Checked = True Then
inssql = "SELECT * FROM tbaccounts WHERE accname='" & txtname.Text & "' " '& " txtname.Text &'"
ElseIf rtell.Checked = True Then
inssql = "SELECT * FROM tbaccounts WHERE acctell= " & txttell.Text
ElseIf rid.Checked = True Then
inssql = "SELECT * FROM tbaccounts WHERE accname= " & txtname.Text
End If

con.Open()
cmdsql.Connection = con
cmdsql.CommandText = inssql
Dim da As New OleDbDataAdapter(inssql, con)

ds.Clear()

da.Fill(ds, "tbaccounts")
DataGridView1.DataBindings.Add(New Binding("datasource", ds, "tbaccounts"))
DataGridView1.Visible = True



con.Close()

راه دوم هم اینه که بهتر جواب میده : می تونی بجای Bind کردن از نمونه کد زیر استفاده کنی ؟


DataGridView1.DataSource = DataSet1.Tables(0)

توی کد خودت :


Dim ds As New DataSet
Dim inssql As String
Dim cmdsql As New OleDbCommand

If rname.Checked = True Then
inssql = "SELECT * FROM tbaccounts WHERE accname='" & txtname.Text & "' " '& " txtname.Text &'"
ElseIf rtell.Checked = True Then
inssql = "SELECT * FROM tbaccounts WHERE acctell= " & txttell.Text
ElseIf rid.Checked = True Then
inssql = "SELECT * FROM tbaccounts WHERE accname= " & txtname.Text
End If

con.Open()
cmdsql.Connection = con
cmdsql.CommandText = inssql
Dim da As New OleDbDataAdapter(inssql, con)

ds.Clear()

da.Fill(ds, "tbaccounts")
DataGridView1.DataSource = ds.Tables(0)
DataGridView1.Visible = True



con.Close()

ali_md110
یک شنبه 27 مرداد 1387, 02:09 صبح
به جای کد خطا دار از کد زیر استفاده کنید

ds=new dataset
DataGridView1.datasource=ds
در ضمن برای جستجو اگر از ارسال پارامتر استفاده کنی مشکلات کمتری دارید

saeednadery
دوشنبه 28 مرداد 1387, 14:50 عصر
دليل آن اين است كه دو بار مي خواهد bind شود و پيغام خطا مي دهد
جهت حل مشكل از نمونه كدزير بجاي bind كردن استفاده كن
( DataGridView1.DataSource = DataSet1.Tables(0

toopak
دوشنبه 28 مرداد 1387, 15:14 عصر
از همه شما دوستای خوب ممنون
مشکلم حل شد