Click here to Skip to main content
12,948,566 members (62,459 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
getting error after converting

private void LoadData()
    {
        con.Open();
      
        string s = "SELECT user_name FROM login" + " WHERE user_name='" + Label1.Text + "'";
        SqlCommand cmd = new SqlCommand(s, con);
        SqlDataReader Dr = cmd.ExecuteReader();
        if (Dr.Read())
        {
            con.Close();
            DateTime datetime = Convert.ToDateTime(Date.Text);
            string query = @"SELECT id,Build_name,Dept,Floor_no,Call_recv FROM  Tech_data where Call_assign='" + Label1.Text + "'  and date_time='" + datetime  + "'";
 
            SqlDataAdapter da = new SqlDataAdapter(query, con);
            DataTable table = new DataTable();
            da.Fill(table);
 
            GridView1.DataSource = table;
            GridView1.DataBind();
        }
 
    }
Posted 23-Feb-13 23:20pm
$ultaNn733
Updated 4-Oct-16 21:27pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

There are a number of reasons for this, but the most common is that the date entered is not in the same format as the current Locale for the computer expects - or the user typed rubbish, of course! :laugh:

This first thing I would do is not use a text box for a Date entry: I would use a DateTimePicker instead if I could as this does not allow the user to enter an invalid date, and it also needs no conversion as it provides a DateTime value directly.

If this wasn't possible, I would use DateTime.TryParse or DateTime.TryParseExact instead of Convert.ToDateTime to eliminate user errors before I opened any connections or did anything else. Verify your data and report errors before you start on processing!
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Use Parameters[^] collection for assigning user input parameters into SQL commands.
You can try this:
string s = "SELECT user_name FROM login WHERE user_name = @u"; // there are no apostrophes around %u!
SqlCommand cmd = new SqlCommand(s, con);
cmd.Parameters.AddWithValue("@u", Label1.Text);

Do the same with date time - just create another command with @d parameter and put it into your SqlDataAdapter instead of plain sql query.

Why are you closing your connection (con) right after Dr.Read() ?
  Permalink  
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

tried this worked fine . thnk you all
 

DateTime datetime = Convert.ToDateTime(Date.Text);
            SqlCommand cmnd = new SqlCommand("SELECT * FROM  Tech_data where Call_assign= @Call_assign and dateadd(dd, datediff(dd,0, [date_time]), 0) = @date_time ");
            cmnd.Connection = con;
            cmnd.Parameters.Add("@Call_assign", SqlDbType.VarChar).Value = Label1.Text;
            cmnd.Parameters.Add("@date_time", SqlDbType.DateTime).Value = datetime.Date;
            SqlDataAdapter da = new SqlDataAdapter(cmnd);
            DataSet ds = new DataSet();
            da.Fill(ds);
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Hi Dear,

Please check that Date.Text is in which format.
If it is in dd/MM/yyyy or dd-MM-yyyy then it will give error while converting string to datetime in below statement.
DateTime datetime = Convert.ToDateTime(Date.Text);

in this case your date should be in MM-dd-yyyy or yyyy-MM-dd

In the other case please change below statement
string query = @"SELECT id,Build_name,Dept,Floor_no,Call_recv FROM Tech_data where Call_assign='" + Label1.Text + "' and date_time='" + datetime + "'";

to

string query = @"SELECT id,Build_name,Dept,Floor_no,Call_recv FROM Tech_data where Call_assign='" + Label1.Text + "' and date_time='" + datetime.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 5,419
CHill60 3,275
Maciej Los 2,778
Jochen Arndt 1,935
ppolymorphe 1,795


Advertise | Privacy | Mobile
Web02 | 2.8.170524.1 | Last Updated 5 Oct 2016
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100