I am creating one application in C# in which I am using datetimepicker control. I have created a database in MS Access which contains one table with Date_Entry, Emp_No, Emp_Name, In_Time, Out_Time columns.
Now I want to retrieve these data in textbox by clicking date on datetimepicker control. This date is point the date in Date_Entry field of database and fetch the data according to that date.
How to do it?
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
try
{
OleDbConnection conn = new OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0,DataSource=C:\\Users\\jd\\Desktop\\Attendance.mdb");
OleDbCommand cmd = new OleDbCommand("SELECT * FROM Attendance_Details WHERE Date_Entry=" + dateTimePicker1.Value + "", conn);
cmd.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "Attendance_Details");
txtDate.Text = ds.Tables[0].Rows[0][0].ToString();
txtEmpNo.Text = ds.Tables[0].Rows[0][1].ToString();
txtEmpName.Text = ds.Tables[0].Rows[0][2].ToString();
txtInTime.Text = ds.Tables[0].Rows[0][3].ToString();
txtOutTime.Text = ds.Tables[0].Rows[0][4].ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
There are some problems in above code. I have refined it. Now it will work.
Following Should be the code:
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
try
{
OleDbConnection conn = new OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0,DataSource=C:\\Users\\jd\\Desktop\\Attendance.mdb");
con.open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM Attendance_Details WHERE Date_Entry=#dtpDate", conn);
cmd.Parameters.Addwithvalue("#dtpDate", dateTimePicker1.Value);
cmd.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
txtDate.Text = ds.Tables[0].Rows[0][0].ToString();
txtEmpNo.Text = ds.Tables[0].Rows[0][1].ToString();
txtEmpName.Text = ds.Tables[0].Rows[0][2].ToString();
txtInTime.Text = ds.Tables[0].Rows[0][3].ToString();
txtOutTime.Text = ds.Tables[0].Rows[0][4].ToString();
con.close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.close();
}
}
}
Hope Its helpful.
To answer your problem about the "Error 1 'System.Data.OleDb.OleDbParameterCollection' does not contain a definition for 'Addwithvalue' and no extension method 'Addwithvalue' accepting a first argument of type 'System.Data.OleDb.OleDbParameterCollection'"
...did you Google? https://www.google.nl/search?q=Addwithvalue It actually shows you what's wrong.
About selecting dates: read this. It can be tricky. Make sure you use the right syntax.
Related
what will be the ms access query for comparing date from database to current date ?
below is the code i have written .
private void RefreshButton_Click(object sender, EventArgs e)
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "SELECT Name FROM Product WHERE Insertiondate = dateTimePicker.Value.Date.ToString()";
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
TodaydataGridView.DataSource = dt;
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
}
You need to pass Date Time picker value as a parameter. Try like:
...
command.CommandText = "SELECT Name FROM Product WHERE Insertiondate = ?";
command.Parameters.Add("#Date, OleDbType.DateTime).Value = dateTimePicker.Value;
...
private void RefreshButton_Click(object sender, EventArgs e)
{
var date = DateTime.Now.Date;
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "SELECT Name FROM Product WHERE Insertiondate = Date()";
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
TodaydataGridView.DataSource = dt;
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
}
I am trying to load the data from MySQL database where the images are also placed in my table but when the form loads it gives the exception:
Any help would be appreciated
private void DealSuggestion_Load(object sender, EventArgs e)
{
try
{
status.Items.Add("active");
status.Items.Add("inactive");
id.Visible = false;
label7.Text = "";
conn.Open();
MySqlCommand cm = new MySqlCommand();
string query = "SELECT * from dealSuggestion where Status='inactive' LIMIT 8";
cm.CommandText = query;
cm.Connection = conn;
MySqlDataAdapter da = new MySqlDataAdapter(cm);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex);
}
}
DataGridViewImageColumn dgvimgcol = new DataGridViewImageColumn();
Check this out
https://www.c-sharpcorner.com/UploadFile/009464/insert-images-into-datagridview-in-windows-application-using/
Columns in datagridview are consider as string so you need to create an imagecolumn instead of binding it
DropDownList needs only one value for one time.
This is my code:
SqlConnection con = new SqlConnection("data source=.;initial catalog=Rupesh;integrated security=true");
SqlCommand cmd;
SqlDataAdapter da;
string query;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
con.Open();
query = "select * from vendor";
cmd = new SqlCommand(query, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
DropDownList1.Items.Add(dr[4].ToString());
}
con.Close();
vendordetails();
}
}
private void vendordetails()
{
try
{
con.Open();
query = "select * from vendor";
cmd = new SqlCommand(query, con);
da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
con.Open();
query = "select * from vendor where vendor_name='" + DropDownList1.SelectedItem.ToString() + "'";
cmd = new SqlCommand(query, con);
da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
Instead of using
"select * from vendor"
use something like
"select [columnname] from vendor"
Replace [columnname] with the name of the column you want to display.
The * gets you all values from all columns from the database.
I think instead of
if (!IsPostBack)
{
query = "select * from vendor";
}
you need
if (!IsPostBack)
{
query = "select distinct vendor_name from vendor";
}
Now I am assuming that when you select a Vendor from the Drop down, you want to see the vendor details in the grid.
You seem to be not far off......
Adjust your proc vendordetails() to be.............
private void vendordetails(string vendorName = "")
{
try
{
con.Open();
query = String.Format( "select * from vendor where vendor_name = \'{0}\'", vendorName);
cmd = new SqlCommand(query, con);
da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
Then in DropDownList1_SelectedIndexChanged simply have....
vendordetails(DropDownList1.SelectedItem.Text)
For some reasons, i need to write my own sqlquery tool. it generally works.
But when i select a table which contains a binary type, it gives me the error i've shown it below.
I used a DatagridView. My code is below.
private void button1_Click(object sender, EventArgs e)
{
string SQL = txtSQL.Text.Trim().ToString();
try
{
gridResult.DataSource = getDataset(SQL).Tables[0];
}
catch (SqlException err)
{
MessageBox.Show("Error : " + err.Message + "-" + err.Number);
}
}
public DataSet getDataset(string SQL)
{
SqlConnection conn = new SqlConnection(connStr);
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = SQL;
da.SelectCommand = cmd;
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds);
conn.Close();
return ds;
}
I wonder, Is there any way to prevent showing binary area or prevent giving error? What should do now? Or do you have any idea how can i detect the type of binary field when loading into the gridview programatically?
I am having a problem in selecting a value from dropdown list. Regardless of whatever I choose only the first value is selected always. Please help...
Here is the code...
protected void Button4_Click(object sender, EventArgs e)
{
SqlConnection con;
String strcon = ConfigurationManager.AppSettings["Constr"].ToString();
try
{
if (!IsPostBack)
{
con = new SqlConnection(strcon);
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select user_name from user_details where role='USER'", con);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList1.DataTextField = "user_name";
DropDownList1.DataSource = ds.Tables[0].DefaultView;
DropDownList1.DataBind();
}
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
protected void Button6_Click(object sender, EventArgs e)
{
string Name = Session["name"].ToString();
SqlConnection con;
String strcon = ConfigurationManager.AppSettings["Constr"].ToString();
try
{
con = new SqlConnection(strcon);
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select user_name,Arival,late,Day_count from attedance where user_name='" + DropDownList1.SelectedItem.Text + "' ", con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
Don't quite get your code. You've shown two button click event handlers.
The first populates the drop-down, so the first item will be selected (that's how it works).
The second one populates a gridview.
If the issue arises from clicking 'Button4' (rename buttons to make it clear what they do), then that's your issue surely?
Also, you're not closing your SqlConnection. Use a using block:
using (SqlConnection connection = new SqlConnection(connectionString))
{
//Do work here
}
EDIT: Ah, just noticed the (!IsPostBack).
Is ViewState enabled for the drop-down?