Albert Ambassy

Albert Ambassy

  • NA
  • 1
  • 1.5k

Validation for Numeric Value

Oct 10 2011 1:42 PM
Hi,

How can we use Validation to enter Numeric value only in ASP.NET...

Answers (2)

0
Iftikar Hussain

Iftikar Hussain

  • 0
  • 12.6k
  • 449.2k
Jul 28 2013 2:29 AM
Here it is

DateTime date = dateTimePicker1.Value.AddDays(-5); // will substract 5 days from selected date
DataTable dt = new DataTable(); //initializing the datatable
using (SqlConnection conn = new SqlConnection(@"Data Source=SQLEXPRESS;Initial Catalog=MyDb;Persist Security Info=True;User ID=MyUserId;Password=MyPassword")) //creating a new sql connection by passing the connection string
{
     if (conn.State == ConnectionState.Closed) //checking the connection state
         conn.Open(); //opening the connection
     string sql= "select *from YourTable where DateColumn>=@Field"; //sql query to select data where DateColumn value is greater than date variable
     SqlCommand cmd = new SqlCommand(sql, conn); //creating the sql command
     cmd.Parameters.AddWithValue("@Field", date); //adding the parameters to the sql query
     SqlDataAdapter dap = new SqlDataAdapter(cmd); //creating data adapter
     dap.Fill(dt); //filling the DataTable
     if (conn.State == ConnectionState.Open)
         conn.Close();
     conn.Dispose(); 
     dataGridView1.DataSource = dt; //binding the datatable to gridview
}

Regards,
Iftikar

Accepted Answer
1
Iftikar Hussain

Iftikar Hussain

  • 0
  • 12.6k
  • 449.2k
Jul 28 2013 2:16 AM
try like this

DateTime date = dateTimePicker1.Value.AddDays(-5);
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(@"Data Source=SQLEXPRESS;Initial Catalog=MyDb;Persist Security Info=True;User ID=MyUserId;Password=MyPassword"))
{
     if (conn.State == ConnectionState.Closed)
         conn.Open();
     string sql= "select *from YourTable where DateColumn>=@Field";
     SqlCommand cmd = new SqlCommand(sql, conn);
     cmd.Parameters.AddWithValue("@Field", date);
     SqlDataAdapter dap = new SqlDataAdapter(cmd);
     dap.Fill(dt);
     if (conn.State == ConnectionState.Open)
         conn.Close();
     conn.Dispose();
     dataGridView1.DataSource = dt;
}

Regards,
Iftikar
1
Vulpes

Vulpes

  • 0
  • 96k
  • 2.6m
Jul 25 2013 11:12 AM
Try:

string command = "SELECT * FROM Table WHERE DateColumn = '" + dateTimePicker1.Value.AddDays(-5).ToString("dd/MM/yyyy") + "'";
0
ta mu

ta mu

  • 0
  • 201
  • 82.5k
Jul 28 2013 2:13 AM
this code is selecting records before last five days.  how i can select records of last five day what thing i need to change because i cant under stand this code. plz explain with comments
if you can
thank you
0
Iftikar Hussain

Iftikar Hussain

  • 0
  • 12.6k
  • 449.2k
Jul 25 2013 11:37 AM
try like this

DateTime date = dateTimePicker1.Value.AddDays(-5);
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(@"Data Source=SQLEXPRESS;Initial Catalog=MyDb;Persist Security Info=True;User ID=MyUserId;Password=MyPassword"))
{
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                string sql= "select *from YourTable where DateColumn<=@Field";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@Field", date);
                SqlDataAdapter dap = new SqlDataAdapter(cmd);
                dap.Fill(dt);
                if (conn.State == ConnectionState.Open)
                    conn.Close();
                conn.Dispose();
                dataGridView1.DataSource = dt;
}

Regards,
Iftikar
0
ta mu

ta mu

  • 0
  • 201
  • 82.5k
Jul 25 2013 10:52 AM
these are the sql qurries i need c# code
  1. like the crrunt time will be "dateTimePicker1.Value" how to  minus 5 day from it and select
  2. records of that days
0
Iftikar Hussain

Iftikar Hussain

  • 0
  • 12.6k
  • 449.2k
Jul 24 2013 9:08 AM
Hi,
        Try like this

select *FROM YourTable DATEDIFF(dd,GETDATE(),YourDateColumn)<=5

Regards,
Iftikar
0
Riyaz Akhtar

Riyaz Akhtar

  • 0
  • 1.1k
  • 411.5k
Jul 24 2013 9:04 AM

use your SQL query like below , 


SELECT * FROM TABLENAME WHERE dateColumnname >=  convert(varchar(10), DATEADD(DAY,  -5, GETDATE()), 120) AND dateColumnname<=GETDATE()


This query will fetch last 5 days records
0
ta mu

ta mu

  • 0
  • 201
  • 82.5k
Jul 24 2013 8:50 AM
i want to select records from database table and populate them in datagrid view in window form
0
Riyaz Akhtar

Riyaz Akhtar

  • 0
  • 1.1k
  • 411.5k
Jul 24 2013 7:54 AM
are you storing date in that table ?