I'm trying to display the name of products in a database, but it crashes on the line da.Fill(dt)
Cb_Produits.Items.Clear()
con.Open();
SqlCommand cmd = con.CreateCommand()
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * From [Transaction]"
cmd.ExecuteNonQuery()
DataTable dt = new DataTable()
SqlDataAdapter da = new SqlDataAdapter()
da.Fill(dt)
foreach (DataRow dr in dt.Rows)
Cb_Produits.Items.Add(dr["Nom_Produit"].ToString())
con.Close()
Can anyone explain why this is happening?
you have to pass your SqlCommand to the SqlDataAdapter constructor ...
...
SqlDataAdapter da = new SqlDataAdapter(cmd);
...
and you should pass your connection to the SqlCommand constructor.
Related
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection(Global.ConnectionString());
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
SqlDataReader drReader = null;
cmd = new SqlCommand();
conn.Open();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "spIndustryDataSelect";
cmd.Parameters.AddWithValue("#iiProcessType", 1);
drReader = cmd.ExecuteReader();
conn.Close();
da.SelectCommand = new SqlCommand();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(dt);
lstIndustry.DataSource = dt;
lstIndustry.DataBind();
I using this c# code to fill the lstIndustry asp ListView, but it take a minute time because it has a thousand of rows and i need to select all, is there any method to fill the list in fast time?
You can create pagination with Using Take And Skip for improve performance
You can add Index on Table if You have where in sp
Hi i have combobox with Table names. I would like to display selected table on button click. How to do it? This is code of my combobox
Con.Open();
SqlCommand cmd = new SqlCommand("SELECT name FROM sys.tables", Con);
SqlDataReader rdr;
rdr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("name", typeof(string));
dt.Load(rdr);
wczytywanie.ValueMember = "name";
wczytywanie.DataSource = dt;
Con.Close();
now i would like to display these tables in dataGridView after button click.This is not working.
Con.Open();
SqlCommand cmd = new SqlCommand("SELECT * from sys.tables where name=#name", Con);
cmd.Parameters.Add("name", wczytywanie.SelectedValue.ToString());
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
Dane.DataSource = dt;
Con.Close();
try adding DisplayMember and assign value "name"
wczytywanie.ValueMember = "name";
wczytywanie.DisplayMember = "name";
This might do
conn = new MySqlConnection(connectString);
conn.Open();
comm = new MySqlCommand(query, conn);
MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn);
DataSet DS = new DataSet();
adapter.Fill(DS);
advancedDataGridView1.DataSource = DS.Tables[0];
conn.Close();
And to change the column name your sql be like columnname as 'your preferred column name'
SELECT principal_id as 'Principal ID' from sys.tables where name=#name"
I want to retrieve data from postgres db into textbox in C# without using grid.
Here is the running successful code by using grid which I had tried:
connection.Open();
NpgsqlCommand cmd = new NpgsqlCommand();
cmd.Connection = connection;
cmd.CommandText = "SELECT * FROM student_folio";
cmd.CommandType = CommandType.Text;
NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
cmd.Dispose();
connection.Close();
GridView1.DataSource = dt;
GridView1.DataBind();
I get error when build when I want to retrieve it into textbox:
"cannot apply indexing with [] to an expression of type 'NpgsqlDataAdapter'"
Here is my block of code:
connection.Open();
NpgsqlCommand cmd = new NpgsqlCommand();
cmd.Connection = connection;
cmd.CommandText = ("SELECT f_name FROM student_folio WHERE id = 1");
cmd.CommandType = CommandType.Text;
NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd);
txtFname.Text = da["f_name"].ToString();
cmd.ExecuteNonQuery();
cmd.Dispose();
connection.Close();
A DataAdapter is not an array of rows you can loop into.
Look at your first code block : you must fill a DataTable from your adapter and then read through the Rows property of this DataTable.
NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
txtFname.Text = dt.Rows[0]["f_name"].ToString();
}
You could also do this :
foreach (System.Data.DataRow row in dt.Rows)
{
txtFname.Text = row["f_name"].ToString();
}
And please, remove the cmd.ExecuteNonQuery(); line, it is useless here
try this . .
connection.Open();
NpgsqlCommand cmd = new NpgsqlCommand();
NpgsqlDataReader dr=null;
cmd.Connection = connection;
cmd.CommandText = ("SELECT f_name FROM student_folio WHERE id = 1");
cmd.CommandType = CommandType.Text;
dr=cmd.ExecuteReader();
while(dr.HasRows)
{
while(dr.Read())
{
txtFname.Text = da["f_name"].ToString();
}
}
connection.Close();
I have created a view called view_SelectAll_Student in SQL Server which retrieves all columns from single table name
And I have a function that returns dataset or datatable using dataadapter somehow I get this error:
The request for procedure 'view_SellectAll_Student' failed because 'view_SellectAll_Student' is a view object.
Code:
public DataTable ViewStudentAll()
{
cons.Open();
DataTable dt = new DataTable();
cmd = new SqlCommand("view_SellectAll_Student", cons);
cmd.Connection = cons;
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
cmd.Dispose();
cons.Close();
adp.Dispose();
return dt;
}
Views still need to be queried. What you have here is just the view name..
So change this:
cmd = new SqlCommand("view_SellectAll_Student",cons);
to this:
cmd = new SqlCommand("SELECT put, columns, here FROM view_SellectAll_Student",cons);
Make sure you put the columns of the view there (or an asterisk.. if you're that way inclined).
Write it like this. If its a view you should SELECT it otherwise you wont get it.
cmd = new SqlCommand("SELECT * FROM view_SellectAll_Student",cons);
cmd.Connection = cons;
//cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
cmd.Dispose();
cons.Close();
adp.Dispose();
return dt;
TIP: While using a DataAdapter there is no need of con.Open() or con.Close() statement. DataAdapter itself will open and close it.
The SqlDataAdapter accepts as first argument an SqlCommand which can
be a Select statement or stored procedure.
In this case you can replace "view_SellectAll_Student" with
"Select * from view_SellectAll_Student"
cons.Open();
DataTable dt = new DataTable();
cmd = new SqlCommand("select * view_SellectAll_Student",cons);
cmd.Connection = cons;
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
cmd.Dispose();
cons.Close();
adp.Dispose();
return dt;
I have two different queries for two different tables I want to show the result in two datagridviews on a form
string query1 = string.Format("select * from Flat where [Flat_No.]='{0}'",flat.Text);
string query2 = string.Format("select * from 1");
SqlCommand cmd = new SqlCommand(query1, con);
SqlCommand cmd1 = new SqlCommand(query2, con1);
dataview frm1 = new dataview(query1,query2); //the form where data is to be displayed
// on form dataview I have two DataGridViews
public dataview(string a,string b)
{
InitializeComponent();
SqlConnection con = new SqlConnection(Class1.getConnectionString);
//connection name
con.Open();
SqlCommand cmd = new SqlCommand(a , con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "ss");
dataGridView1.DataSource = ds.Tables["ss"];
con.Close();
SqlConnection con1 = new SqlConnection(Class1.getConnectionString);
//connection name for query1
con1.Open();
SqlCommand cmd1 = new SqlCommand(b, con1);
cmd1.CommandType = CommandType.Text;
SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
DataSet ds1 = new DataSet();
da.Fill(ds1, "aa");
dataGridView2.DataSource = ds1.Tables["aa"];
con1.Close();
}
}
but the above code is showing data from query 1 in both the datagridviews. plz help me out how can I solve this problem? If there in another way let me know it also. I have also tried to merge both the queries using "+" sign but it also didn't proved helpful.
Use da1.Fill instead of da.fill. You're using the da DataAdapter for filling both Datasets
da.Fill(ds1, "aa");
da1.Fill(ds1, "aa");