Use from dataReader to bind data to a data gridview
-
Hi, i am loading my data with a dataReader but every time i am select another table, the new selected Rows will add below the previous table Rows. how can i empty it and load Only data of the new selected table. thank you in advance. here is my code: OleDBConnectio con=new OleDBConnection(conectionString); con.Open(); OleDBDataCommand com=new OleDBCammand(“Select *From[“+txtBox1.Text+”]”,con); OleDBDataReader dr; dr=com.ExecuteReader(); While(dr.read()) { Dgv1.Rows.Add(dr[“ID”],dr.[“name”].ToString()); } dr.Close(); con.Close(); i should mention that i don't want use from data source to bind my database to DGV, because i want to add row in my DGV programmatically. thanks a lot.
-
Hi, i am loading my data with a dataReader but every time i am select another table, the new selected Rows will add below the previous table Rows. how can i empty it and load Only data of the new selected table. thank you in advance. here is my code: OleDBConnectio con=new OleDBConnection(conectionString); con.Open(); OleDBDataCommand com=new OleDBCammand(“Select *From[“+txtBox1.Text+”]”,con); OleDBDataReader dr; dr=com.ExecuteReader(); While(dr.read()) { Dgv1.Rows.Add(dr[“ID”],dr.[“name”].ToString()); } dr.Close(); con.Close(); i should mention that i don't want use from data source to bind my database to DGV, because i want to add row in my DGV programmatically. thanks a lot.
-
Hi, thank you legitimate programmer. thanks a million for your help:thumbsup::thumbsup::thumbsup::rose:
-
Hi, i am loading my data with a dataReader but every time i am select another table, the new selected Rows will add below the previous table Rows. how can i empty it and load Only data of the new selected table. thank you in advance. here is my code: OleDBConnectio con=new OleDBConnection(conectionString); con.Open(); OleDBDataCommand com=new OleDBCammand(“Select *From[“+txtBox1.Text+”]”,con); OleDBDataReader dr; dr=com.ExecuteReader(); While(dr.read()) { Dgv1.Rows.Add(dr[“ID”],dr.[“name”].ToString()); } dr.Close(); con.Close(); i should mention that i don't want use from data source to bind my database to DGV, because i want to add row in my DGV programmatically. thanks a lot.
Member 13325846 wrote:
new OleDBCammand(“Select *From[“+txtBox1.Text+”]”,con)
That code is vulnerable to SQL Injection[^]. Unfortunately, there's no easy way to fix it, since you can't use a parameter as a table name. Rather than letting the user type in any table name, it would be better to give them a drop-down list of tables to choose from. After all, not every table in your database is going to have
Id
andName
columns. It would also be better to only load the specific columns you need, rather than usingSELECT * FROM ...
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Member 13325846 wrote:
new OleDBCammand(“Select *From[“+txtBox1.Text+”]”,con)
That code is vulnerable to SQL Injection[^]. Unfortunately, there's no easy way to fix it, since you can't use a parameter as a table name. Rather than letting the user type in any table name, it would be better to give them a drop-down list of tables to choose from. After all, not every table in your database is going to have
Id
andName
columns. It would also be better to only load the specific columns you need, rather than usingSELECT * FROM ...
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Hi, Thank You For your care. Yes your right and this was just an example and I am using from a cboBox. Thanks a million:rose: