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;
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
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.
Here is my code,
Conn.Open();
DataTable dt = new DataTable();
DataSet ds = new DataSet();
SqlCommand sqlCmd = new SqlCommand("SELECT * from CurrentDataCR ",Conn);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlDa.Fill(ds);
ds.Tables[0].DefaultView.RowFilter = " mst_remote_station_id Like'*9001*'";
Here I am getting Complete row for id 9001. I need only one column value for this id.
DataRow[] rows = ds.Tables[0].Select("mst_remote_station_id Like '%9001%'");
You can do it this way also if you need only one row just select it in the initial query.
Also you should Dispose the SqlDataAdapter after using it ! You can do it with using block
Conn.Open();
DataSet ds = new DataSet();
SqlCommand sqlCmd = new SqlCommand("SELECT * from CurrentDataCR ",Conn);
using(SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd))
{
sqlDa.Fill(ds);
}
ds.Tables[0].Select("mst_remote_station_id Like '%9001%'");
I don't know if the connection is global but it is bad practice to use global connection, you have connection pool so use separate connection for every query.
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");
I can't understand what I am doing wrong, I can't seem to SELECT with a prepared statement. However I can INSERT with a prepared statement.
MySqlCommand cmd = new MySqlCommand("SELECT * FROM code_post WHERE name = ?postRequired LIMIT 1", dbcon);
cmd.Parameters.Add(new MySqlParameter("?postRequired", requestString));
cmd.ExecuteNonQuery();
DataSet ds = new DataSet();
cmd.fill(ds, "result");
try {
thisBlog = ds.Tables["result"].Rows[0];
} catch {
invalid();
return;
}
Any advice on this would be greatly appreciated!
To fill a DataSet you will need a DataAdapter.
Try this:
MySqlCommand cmd = new MySqlCommand("SELECT * FROM code_post WHERE name = ?postRequired LIMIT 1", dbcon);
cmd.Parameters.Add(new MySqlParameter("?postRequired", requestString));
cmd.ExecuteNonQuery();
DataSet ds = new DataSet();
MySqlDataAdapter dAdap = new MySqlDataAdapter();
dAdap.SelectCommand = cmd;
dAdap.Fill(ds, "result");
try {
thisBlog = ds.Tables["result"].Rows[0];
} catch {
invalid();
return;
}
You need to use SqlDataAdapter
DataAdapter represents a set of data commands and a database connection that are used to fill the DataSet and update a SQL Server database.
The SqlDataAdapter provides this bridge by mapping Fill, which changes the data in the DataSet to match the data in the data source
Check the following syntax:
private static DataSet SelectRows(DataSet dataset,
string connectionString,string queryString)
{
using (SqlConnection connection =
new SqlConnection(connectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(
queryString, connection);
adapter.Fill(dataset);
return dataset;
}
}