Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Text box entry shows this error.

DESIGN PART
Here I'm using ajaxCalender control.
XML
asp:TextBox ID="txtIssue" runat="server" Width="267px"></asp:TextBox>
            <asp:CalendarExtender ID="calissue" TargetControlID="txtIssue" runat="server" Format="dd/MM/yyyy" Enabled="true" ></asp:CalendarExtender>


CODE PART
string str="Insert into tbl_issue (issue_date) values ('"+txtIssue.Text+"')";

when I want to insert the date,it through this error.

Thanks & Regards
Bigyan Ranjan Sahoo
Posted

use parameters
C#
string str="Insert into tbl_issue (issue_date) values (@issue_date)";

then you can set the parameter value as below
C#
cmd.Parameters.AddWithValue("@issue_date", CalendarExtender1.SelectedDate.Date);
 
Share this answer
 
v3
So check the user input before you try passing it through to SQL. This has a number of advantages.
1) You can check the number using the user's preferred input format: mm/dd/yyyy or dd/mm/yyyy for example.
2) You can tell the user exactly what he did wrong and let him correct it before you enter corrupt data to yoru DB.
3) Your user can't delete your database by typing in the text box. Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

So check it (basic version, doesn't use user locale):
C#
DateTime dt;
if (!DateTime.TryParse(txtIssue.Text, out dt))
   {
   // Report problem to user
   ...
   return;
   }
...
SqlCommand cmd = new SqlCommand("INSERT INTO tbl_issue (issue_date) VALUES (@DT)", con);
cmd.Parameters.AddWithValue("@DT", dt);
...
 
Share this answer
 
v2

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