1
Answer

How to programmatically check a checkbox?

Consider the following table

Name value
aaa 1
bbb 0
ccc 1
Now I am trying to generate a report(html form) through my web page. When the report loads I want it to show 3 checkbox and the ones with value "1" in the database should be automatically checked. For eg-

aaa checked
bbb unchecked
ccc checked
Answers (1)
0
Amit Choudhary

Amit Choudhary

NA 27.7k 3m 14y
well param1 is an object of SqlParameter class that helps to map the .Net Type data to the Sql Types data.

Hope it make sense.
0
vishnuvardhan diyyala

vishnuvardhan diyyala

NA 38 90.5k 14y

  Dear amit,
        By your example i got my mystake. But im unable to resolve that. y because i m a beginer(student) in .net. so unable to catch u r solution.
SqlParameter param1  = new SqlParameter();
 param.ParameterName = "@NewDate";

and
SqlParameter param2  = new SqlParameter();
 param.ParameterName = "@OldDate";
what is param1,parameter name in second line of above two sentences given by u.My database table name is totallogins.
on that table
id--------Auto no
tokenno----text
userid-----number
fromdate----date/time    and
todate----date time.
Could you pleae explain me about parameter method
Thanks in advance.
0
Amit Choudhary

Amit Choudhary

NA 27.7k 3m 14y
Thanks for clarification suthish...
0
Suthish Nair

Suthish Nair

NA 31.7k 4.6m 14y

1. Passing datetime as a string value.

2. Due to that Datatype mismatch happening.

3. SQLParameter will take care of all the formats between SQL DB table and your commands.

4. Best way if your DB table datatype column is DateTime then,
SqlParameter param = cmd.Parameters.Add("@datetime", System.Data.SqlDbType.DateTime).Value = DateTime.Now;

5. Do not convert to string value, can use Convert.ToDateTime() method.
0
Amit Choudhary

Amit Choudhary

NA 27.7k 3m 14y
I have faced the same problem a long time ago.. when i was try to insert the date into database using DateTime.Now.

Suthish is right by using the Parametrized query your problem will be solve.
 Here is the sample:
string st6 = "update totallogins set To_date =@NewDate

where from_date=@OldDate;
 cmd.CommandText = st6;
 SqlParameter param1  = new SqlParameter();
 param.ParameterName = "@NewDate";
 param.Value         = DateTime.Now;

 SqlParameter param2  = new SqlParameter();
 param.ParameterName = "@OldDate";
 param.Value         = b;

 cmd.Parameters.Add(param1);
  cmd.Parameters.Add(param2);
 cmd.Connection = con;
 con.Open();
 cmd.ExecuteNonQuery();
 con.Close();


Now Suthish i have question here and need your input.. why DateTime Could'nt save directly using the command object ?? Why do we need to use parameter to save the Date using DateTime class.

Thanks.

0
Suthish Nair

Suthish Nair

NA 31.7k 4.6m 14y

This not a good way of coding.. Leads to SQL Injection, always use Parameterized Query.

This will solve your problem, give same datatype during creating parameters.

Parameterized Query and SQL Injection Attacks