Click here to Skip to main content
16,017,638 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
C#
protected void Button1_Click(object sender, EventArgs e)
{
    int enroll = int.Parse(enrl.Text);
    string birth = Convert.ToDateTime(calendar.Text).ToShortDateString();           
    string fullname = name.Text;
    string father = fname.Text;
    string Occ = fOcc.Text;
    string mname = mothername.Text;
    string school = prevschool.Text;
    string gen = dropGen.Text;
    int mobile = int.Parse(mob.Text);
    string mail = email.Text;
    string rel = DropRelgn.Text;
    string adm = Convert.ToDateTime(admDate.Text).ToShortDateString();
    
    string facility = hostel.Text;
    string ad = address.Text;
    
    cmd.CommandText = "insert into Registration_N(enroll,FullName,AdmissionDT,DOB,Religion,Gender,Father,FOccup,address,Mobile,dayHostel,previousSchool,motherName,email)  values (" + enroll + ",'" + fullname + "','" + adm + "', '" + birth + "' , "+rel+","+father+" , "+fOcc+","+ad+",'"+mobile+"',"+hostel+","+prevschool+","+mname+","+email+")";
    cmd.ExecuteNonQuery();
    da.Update(ds,"Registration_N");
    Label1.Text = "RECORD ADDED";
    con.Close();
}
Posted
Updated 16-Jun-15 21:48pm
v2
Comments
Sinisa Hajnal 17-Jun-15 3:50am    
Several things:
1. this is not a good question: you didn't provide us with the date you're passing (exact string), also the cultureinfo of the process
2. NEVER pass user input directly into your query (google SQL Injection)
3. use finally block to dispose of objects so that exception doesn't leave them hanging. Side-benefit is catching and handling exceptions instead of app failing totally.
MayankSemwal 17-Jun-15 4:33am    
i am passing date through user
Sinisa Hajnal 17-Jun-15 4:58am    
Yes, you are, but WHAT date? In which format? For which culture?
MayankSemwal 17-Jun-15 5:07am    
format is this DD-MM-YYYY
Tomas Takac 17-Jun-15 5:39am    

Firstly, 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

Secondly, why the heck are you taking a string based date, converting it to a DateTime, converting it back to a string, and passing it to Access in order for it to convert it right back to a DateTime value again?

Do yourself a favour: if you must use textbox for date based entries (and I wouldn't) then write your code so that it checks if the user has entered a valid date. If you don't then you will get an application error "String was not recognized as a valid DateTime" - which I'm sure you have already noticed.

Check the user input using DateTime.TryParse, and use a parameterised quyery to pass it to Access only if it is valid information.
C#
DateTime DOB;
if (!DateTime.TryParse(calender.Text, out DOB))
   {
   // Report a problem to the user
   ...
   return;
   }
...
 
Share this answer
 
instead of using this -
string adm = Convert.ToDateTime(admDate.Text).ToShortDateString();

Use This one -
string admitDate = admDate.Text;
string adm =Convert.ToDateTime(admitDate);

and in database use datatype datetime for admitDate Field..

Thanks in Advance...
 
Share this answer
 
Comments
MayankSemwal 17-Jun-15 4:39am    
Still throwing the same error..
Sinisa Hajnal 17-Jun-15 4:57am    
This will fail just as the original, the cast is the problem not the database.
MayankSemwal 17-Jun-15 5:07am    
Can u help me solve it..
Sinisa Hajnal 17-Jun-15 8:14am    
Yes, but you have to provide the data I asked (see my comment under your question).

Also, OriginalGriff gave you good solution.

Think also about using masked textbox, datetime pickers or other specialized controls (or at least offer the calendar control in a little dialog for those who fail to enter correct date).

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