well, I've done everything on my behalf just to solve this problem. i'm trying to print the data from my database using grid view in asp.net using c# codes. can anyone tell me whats wrong and how to improve my codes. thank you.
using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["DBCon"].ConnectionString))
{
constructor var = new constructor();
con.Open();
string sql = "SELECT first_name,last_name,username,contact_number,address,email FROM user_tbl WHERE user_type='2'";
MySqlCommand cmd = new MySqlCommand(sql, con);
MySqlDataReader reader1 = cmd.ExecuteReader();
reader1.Close();
try
{
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception ex)
{
lblresult.Text = "ERROR>>" + ex.Message + "!";
}
finally
{
con.Close();
sql = null;
}
You must fill the DataSet with data like this :
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "TableName");
GridView1.DataSource = ds.Tables["TableName"];
GridView1.DataBind();
You're assigning an empty DataSet to your DataSource, without filling the results of your DataReader into the DataSet/DataTable.
using (MySqlConnection con = new MySqlConnection(""))
{
con.Open();
string sql = "SELECT first_name,last_name,username,contact_number,address,email FROM user_tbl WHERE user_type='2'";
MySqlCommand cmd = new MySqlCommand(sql, con);
try
{
DataTable dt = new DataTable();
using (MySqlDataReader reader1 = cmd.ExecuteReader())
{
dt.Load(reader1);
}
GridView1.DataSource = dt ;
GridView1.DataBind();
}
catch (Exception ex)
{
lblresult.Text = "ERROR>>" + ex.Message + "!";
}
finally
{
con.Close();
sql = null;
}
}
Related
I'm trying to update datatable in the database using a SqlDataAdapter. I don't see any errors and also it's not updating the database.
I tried other solutions in the stackoverflow but still no luck. Here is my code:
public void Update(DataTable dt, String tableName)
{
try
{
SqlDataAdapter da = new SqlDataAdapter();
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
da.SelectCommand = new SqlCommand("SELECT * FROM " + tableName, connection);
SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(da);
da.UpdateCommand = cmdBuilder.GetUpdateCommand(true);
da.AcceptChangesDuringUpdate = true;
int res = da.Update(dt);
}
}
catch (Exception oEx)
{
System.Diagnostics.Debug.WriteLine("Ex: " + oEx);
}
}
Thanks in advance for your help!
What is my method missing? It is not returning the data table.
public DataTable GetHotelReportData(int _ratpropid) {
var _connectionString = _isDevelopment ? CommonTypes.Dev : CommonTypes.Prod;
DataTable dt = new DataTable();
dt.Clear();
SqlConnection conn = new SqlConnection(_connectionString);
conn.Open();
SqlCommand cmd = new SqlCommand("ww.HotelRpt_spGenerateData2018", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#RatPropId", _ratpropid);
SqlDataAdapter da = new SqlDataAdapter();
try {
da.SelectCommand = cmd;
da.Fill(dt);
}
catch (Exception _ex) {
new ErrorLogging().Log(_ex);
}
finally {
conn.Close();
da.Dispose();
cmd.Dispose();
}
return dt;
}
Try cutting the data adapter out and just using the command. No need for an adapter as far as I can tell:
public DataTable GetHotelReportData(int _ratpropid) {
var _connectionString = _isDevelopment ? CommonTypes.Dev : CommonTypes.Prod;
DataTable dt = new DataTable();
dt.Clear();
SqlConnection conn = new SqlConnection(_connectionString);
SqlCommand cmd = new SqlCommand("ww.HotelRpt_spGenerateData2018", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#RatPropId", _ratpropid);
try {
conn.Open();
dt.Load(cmd.ExecuteReader);
return dt;
}
catch (Exception _ex) {
new ErrorLogging().Log(_ex);
}
finally {
conn.Close();
cmd.Dispose();
}
}
If that doesn't work, try executing your stored procedure natively in SSMS (or another DBMS) to see if you get any results there.
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)
I have datagridview, that i must fill by 5 tables. I declared SqlCommand and SqlConnection.
After that I use somethine like this:
selCommand.Connection = conn;
dt = new DataTable();
SqlDataAdapter ad = new SqlDataAdapter();
ad.SelectCommand = selCommand;
ad.Fill(dt);
dataGridView1.DataSource = dt;
As a result I have column headers of my query in datagridview, but don't have data.
I tried use this code:
selCommand.Connection = conn;
dt = new DataTable();
SqlDataReader dr = selCommand.ExecuteReader();
dt.Load(dr);
bs = new BindingSource();
bs.DataSource = dt;
dataGridView1.DataSource = bs;
dr.Close();
It was working, but I something change and I can't understand why it does not work.
Try this:
DataTable table = null;
using (SqlConnection connection = new SqlConnection(this.connectionString))
{
try
{
connection.Open();
SqlCommand cmd = connection.CreateCommand();
cmd.CommandText = "SELECT * FROM Something WHERE Id = #Id";
cmd.Parameters.Add(new SqlParameter("#Id", YourValue));
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
table = new DataTable();
adapter.Fill(table);
}
}
catch (Exception ex)
{
//Handle your exception;
}
}
dataGridView1.DataSource = table;
I want to get the result from a query in my oracle database and put it in a gridview. Now my problem is, I have no idea how to output it in the gridview. I am using the gridview from the toolbox and my oracle connection is working. I also have the right SELECT query and I can output that in a listbox. I just have no idea how to do this in a gridview. I looked for it and I came across this: How to populate gridview with mysql? Although this doesn't help me.
How can I output it in a gridview so that it looks exactly the same as a normal table in the oracle database?
What should I use and how?
This is my code:
public void read()
{
try
{
var conn = new OracleConnection("")
conn.Open();
OracleCommand cmd = new OracleCommand("select * from t1", conn);
OracleDataReader reader = cmd.ExecuteReader();
DataTable dataTable = new DataTable();
while (reader.Read())
{
var column1 = reader["vermogen"];
column = (column1.ToString());
listBox1.Items.Add(column);
}
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
To bind a DataTable to a DataGridView your code need simply to be changed to
public void read()
{
try
{
using(OracleConnection conn = new OracleConnection("....."))
using(OracleCommand cmd = new OracleCommand("select * from t1", conn))
{
conn.Open();
using(OracleDataReader reader = cmd.ExecuteReader())
{
DataTable dataTable = new DataTable();
dataTable.Load(reader);
dataGridView1.DataSource = dataTable;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
The OracleDataReader could be passed to the Load method of the DataTable and then the table is ready to be bound to the DataGridView DataSource property. I have also added some using statement to ensure proper disposing of the disposable objects employed. (In particular the OracleConnection is very expensive to not close in case of exceptions)
You can use DataSet too:
public void read()
{
try
{
OracleConnection conn = new OracleConnection("");
OracleCommand cmd = new OracleCommand("select * from t1", conn);
conn.Open();
cmd.CommandType = CommandType.Text;
DataSet ds = new DataSet();
OracleDataAdapter da = new OracleDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
First establish connection in case, you didnt establish globally by using connection string. Then use oleDbcommand for the oracle sql command you want to execute. In my case, it is 'select * from table_name' which would show all data from table to datagrid. I wrote this code in a button to display data on data grid.
{
OleDbConnection conn = new OleDbConnection("");
OleDbCommand cmd = new OleDbCommand("select * from table_name", conn);
{
conn.Open();
OleDbDataReader reader = cmd.ExecuteReader();
{
DataTable dataTable = new DataTable();
dataTable.Load(reader);
dataGridView1.DataSource = dataTable;
}
conn.Close();
}
}