Actually i want data from database in DataGridView, OnLoad of Form. I have write this code but this is not working. I have checked that connection established correclty and query is also wrtie but where is problem?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
namespace DISM_Project1
{
public partial class AvalForm : Form
{
private SqlConnection xSqlConnection;
private SqlCommand xSqlCommand;
private SqlDataAdapter xSqlDataAdapter;
public AvalForm()
{
InitializeComponent();
}
private void AvalForm_Load(object sender, EventArgs e)
{
try
{
//Making Connection
xSqlConnection = new SqlConnection("Data Source=WaQaS-PC;Initial Catalog=BookingSystem;User ID=sa;Password=123");
xSqlConnection.Open();
}
catch (Exception ex)
{
MessageBox.Show("" + ex);
}
xSqlCommand = new SqlCommand("Select Pno, PStatus from Unit1_Table where PStatus = 'Availible' UNION Select Pno, PStatus from Unit2_Table where PStatus = 'Availible'", xSqlConnection);
//SqlDataReader xSqlDataReader = null;
//xSqlDataReader = xSqlCommand.ExecuteReader();
//var objDataGridView = new DataGridView();
dataGridView1.DataSource = xSqlCommand;
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//dataGridView1.DataSource = xSqlCommand;
}
}
}