using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
public partial class DispalyData : System.Web.UI.Page
{
string strcon = ConfigurationManager.ConnectionStrings["TestConnectionString"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("Select * from ws_Students where DOB=@DOB", con);
SqlParameter param = new SqlParameter("@DOB",DbType.DateTime);
param.Value = DateTime.Now.ToString();
cmd.Parameters.Add(param);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
con.Close();
}
}
This is the code where I want to display ID, Name, Scholar from table ws_Students and also added DOB as parameter in a Gridview. But error is not showing and gridview is not displayed in browser. Is there any thing to add in database or in cs file?
Please provide solution . Thanks in Advance.
Abhishek KumarPosted Oct 3, 2014, 5:14 AM
Your does not have any problem it will work fine.
Seems below code you are passing as string but not sure in database table ws_Students fieldDOB is varchar or date time.
param.Value = DateTime.Now.ToString();
So you need to check at db level and try to match the parameter value with database DOB field .
code will work.
Hope this will help.
Abhishek
Vithal WadjePosted Oct 2, 2014, 2:06 PM
Anupam SinghPosted Oct 2, 2014, 11:24 AM
you cant fill grid from DataReader directly, instead you can use data set or DataTable.
try