Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0 C#4.0
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 24-Feb-13 0:20am
Lubnash838
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 | :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 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 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 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
0 Kornfeld Eliyahu Peter 169
1 George Jonsson 145
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,382


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2014
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