Click here to Skip to main content
15,902,447 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
//i am trying to give an output of data showing that if a user enters a name selects the date to his desired date output must be showing name records from date selected her is my code

C#
private void button5_Click(object sender, EventArgs e)
       {

           con = new SqlConnection(@"Data Source=abbas-PC\SQLEXPRESS;Initial Catalog=EREGISTRY;Integrated Security=True");
           ds = new DataSet();
           cmd = new SqlCommand("select * from REGISTRY where CUSTUMER=@CUSTUMER,DATE=@DATE",con);
           cmd.Parameters.Add("CUSTUMER", SqlDbType.NVarChar, 40).Value = textBox6.Text;
           //cmd = new SqlCommand("select * from REGISTRY where DATE=@DATE", con);
           cmd.Parameters.Add("DATE", SqlDbType.NVarChar, 20).Value = dateTimePicker2.Value;
           da = new SqlDataAdapter(cmd);

           con.Open();
           da.Fill(ds, "reg");
           con.Close();


           dataGridView1.DataSource = ds.Tables[0];
Posted

You can't write "CUSTUMER=@CUSTUMER,DATE=@DATE".

You should write "CUSTUMER=@CUSTUMER AND DATE=@DATE"

Another thing as that you should treat dates as Date. Not convert it to strings.
 
Share this answer
 
Comments
[no name] 27-Jul-12 21:21pm    
5. And use using blocks.
StianSandberg 27-Jul-12 21:34pm    
using(){thank you..}
AshishChaudha 28-Jul-12 1:24am    
my 5!
C#
private void button5_Click(object sender, EventArgs e)
       {

           con = new SqlConnection(@"Data Source=abbas-PC\SQLEXPRESS;Initial Catalog=EREGISTRY;Integrated Security=True");
           ds = new DataSet();
           cmd = new SqlCommand("select * from REGISTRY where CUSTUMER=@CUSTUMER and DATE=@DATE",con);
           cmd.Parameters.Add("CUSTUMER", SqlDbType.NVarChar, 40).Value = textBox6.Text;
           //cmd = new SqlCommand("select * from REGISTRY where DATE=@DATE", con);
           cmd.Parameters.Add("DATE", SqlDbType.NVarChar, 20).Value = dateTimePicker2.Value;
           da = new SqlDataAdapter(cmd);

           con.Open();
           da.Fill(ds, "reg");
           con.Close();


           dataGridView1.DataSource = ds.Tables[0];
 
Share this answer
 
correct this line in query

C#
cmd = new SqlCommand("select * from REGISTRY where CUSTUMER=@CUSTUMER,DATE=@DATE",con);

to
C#
cmd = new SqlCommand("select * from REGISTRY where CUSTUMER=@CUSTUMER and DATE=@DATE",con);

in place of , use and
 
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