Thursday, 15 September 2011

c# - Finding Specific Data from Database? -



c# - Finding Specific Data from Database? -

i coding in c#. want search name. , want display details of person database connected to. getting is

error " utilize of unassigned local variable.

i tried initialize variable too. executes shows no output.

here's code...

public partial class _default : system.web.ui.page { protected void button1_click(object sender, eventargs e) { sqlconnection cn = new sqlconnection("server=mithilesh-pc\\server101;initial catalog=people;integrated security=true"); string find = " "; textbox1.text = find; string querystring = "select * members name='{find}' "; sqlcommand cmd = new sqlcommand(querystring,cn); sqldatareader dr = null; cn.open(); dr = cmd.executereader(); gridview1.datasource = dr; gridview1.databind(); cn.close(); } }

string querystring = "select * members name=@name "; sqlcommand cmd = new sqlcommand(querystring,cn); cmd.parameters.addwithvalue("@name", textbox1.text);

add value parameter of sqlcommand, right way. in case prevent sql injection attacks too.

also in begging of code:

string find = " "; textbox1.text = find;

here making text of checkbox equals " ". if want store textbox value in variable find, should write this: string find = textbox1.text;

c# asp.net

No comments:

Post a Comment