Click here to Skip to main content
15,886,639 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, i want to automatically take values from a field in my table and put it in a dropdonlist. I have been able to do this very well but my problem is putting the same thing i have doing before into a datagrid control. This is my code when i have not put it in my datagrid and it work.



strsql="select * from subjects where sch_name='"& lblschool.text &"'"
strcomm="server=(local);database=osr;uid=godwin;pwd=godesky;"
data2=new sqldataadapter (strsql,strcomm)
data2.fill(display3,"subjects")
drpsubject3.datasource=display.tables("subjects").defaultview
drpsubject3.datatextfield="sbj_name"
drpsubject3.datavaluefield="sbj_name"
drpsubject3.databind()


Please i need help on this on how to put it in a datagrid.
Posted

1 solution

wrote:
strsql="select * from subjects where sch_name='"& lblschool.text &"'"


Your database will perform better if you use numeric ids for your searches. Your school names would ideally be in a table, with ids, which you use elsewhere. This also means you don't duplicate data, and it's easy to change a school name.

I see you're using a label here, but if you build SQL like this where the user can enter the text, your database will not be secure, and can easily be deleted, or corrupted. This is called SQL Injection.

I am not sure why you posted your code that works. I can see it will work. What is your code that is broken ? All you need to do, is set your datasource to be the table from your SQL request. I am not sure if a datagrid generates columns by default. A gridview does. If a datagrid does not, you may well have to define templates in your ASPX to define how the data will be shown, which is analagous to your setting the text and value field in your drop list. As a grid shows many values, there is no value and text field, there's a template in the aspx that defines all of the data to show, and how it is shown. Of course, you still need to databind as well.

I would suggest you buy a basic ASP.NET book and read it, as they all cover this stuff in depth.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900