Code:
CREATE FUNCTION [dbo].[LISTSTAFF]()
RETURNS TABLE
AS
RETURN(select * from tblStaff);In C#2005 I assign staff style table for gridview an error, can you help me fix this ?
Code:
...
SqlCommand cmd = new SqlCommand("LISTSTAFF", cnn);
cmd.CommandType = CommandType.StoredProcedure;
gridview.DataSource = cmd.ExecuteReader();// waring error in here
...
Ranjit PowarRank #553 · 4.4K reputationPosted Jun 20, 2014, 12:18 AM EDT
ExecuteReader() return datareader. And you cant assign datareader to DataSource property.
If you want to assign rows from reader to gridview then use Read() method of DataReader and add rows one by one.
Abhishek KumarRank #510 · 4.8K reputationPosted Jun 20, 2014, 12:41 AM EDT
SqlDataReader dr = cmd.ExecuteReader();
if (dr .HasRows)
{
}
Hope this helps.
Abhishek
Dong Lam TrienRank #1,538 · 1K reputationPosted Jun 20, 2014, 8:05 PM EDT
[CODE]
SqlDataReader dr = cmd.ExecuteReader();
if (dr .HasRows)
{
gridview1.DataSource=dr; // warning error in here
gridview1.databind();
}
[/CODE]
Ranjit PowarRank #553 · 4.4K reputationPosted Jun 20, 2014, 8:19 PM EDT
SqlCommand cmd = new SqlCommand("LISTSTAFF", cnn);cmd.CommandType = CommandType.StoredProcedure;
DataSet ds=new DataSet();
DataAdapter da=new DataAdapter(cmd,cnn);
da.Fill(ds);
gridview.DataSource =ds.Tables[0];Dong Lam TrienRank #1,538 · 1K reputationPosted Jun 20, 2014, 8:40 PM EDT
Error 1 The best overloaded method match for 'System.Data.SqlClient.SqlDataAdapter.SqlDataAdapter(string, string)' has some invalid arguments, error = new DataAdapter(cmd,cnn)
Error 2 Argument '1': cannot convert from 'System.Data.SqlClient.SqlCommand' to 'string',... error = cmd
Error 3 Argument '2': cannot convert from 'System.Data.SqlClient.SqlConnection' to 'string' error = cnn
[CODE]
SqlCommand cmd = new SqlCommand("LISTSTAFF", cnn);cmd.CommandType = CommandType.StoredProcedure;
DataSet ds=new DataSet();
DataAdapter da=new DataAdapter(cmd,cnn); // warning error in here
da.Fill(ds);gridview.DataSource =ds.Tables[0];[/CODE]Ranjit PowarRank #553 · 4.4K reputationPosted Jun 20, 2014, 8:49 PM EDT
DataAdapter da=new DataAdapter(cmd);
Dong Lam TrienRank #1,538 · 1K reputationPosted Jun 21, 2014, 12:55 AM EDT
[CODE]
try
{
SqlCommand cmd = new SqlCommand("LISTSTAFF", cnn);cmd.CommandType = CommandType.StoredProcedure;
DataSet ds=new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds); // warning error in here
gridview.DataSource =ds.Tables[0];cnn.Close();}
catch (SqlException ex)
{
cnn.Close();
MessageBox.Show("Error: " + ex.Message);
}
[/CODE]
Ranjit PowarRank #553 · 4.4K reputationPosted Jun 21, 2014, 6:28 AM EDT
Try this
DataSet ds=new DataSet();
SqlDataAdapter da = new SqlDataAdapter("Select *from LISTSTAFF()",cnn);
da.Fill(ds);
gridview.DataSource =ds.Tables[0];Dong Lam TrienRank #1,538 · 1K reputationPosted Jun 21, 2014, 4:09 PM EDT
Ranjit PowarRank #553 · 4.4K reputationPosted Jun 21, 2014, 9:32 PM EDT
Dong Lam TrienRank #1,538 · 1K reputationPosted Jun 22, 2014, 8:38 PM EDT
Ranjit PowarRank #553 · 4.4K reputationPosted Jun 22, 2014, 8:46 PM EDT
Dong Lam TrienRank #1,538 · 1K reputationPosted Jun 22, 2014, 10:31 PM EDT
sudipta sanyalRank #1,945 · 753 reputationPosted Jun 22, 2014, 10:50 PM EDT
CREATE FUNCTION [dbo].[fn_test]()
RETURNS @temp_table TABLE
(
id int,
name nvarchar(80)
)
AS
BEGIN
INSERT @temp_table
SELECT top 10 id, name
FROM your_table
ORDER by id ASC
RETURN
END
GO
NOW go to your .cs page
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter("select * from [fn_test]()", "your_conn_string");
sda.Fill(dt);
Dong Lam TrienRank #1,538 · 1K reputationPosted Jun 25, 2014, 8:03 PM EDT
Palle TechnologiesRank #6,639 · 260 reputationPosted Jul 14, 2014, 6:50 AM EDT
gridView.DataSource=cmd.ExecuteReader();
gridView.DataBind();
Your User defined function is also correct and i dint identified any issues with it . Instead of using UDF it would be better if you use stored procedure.
Jignesh TrivediRank #16 · 50.3K reputationPosted Jul 14, 2014, 9:21 PM EDT
Hi,
here your are creating SQL function and trying to fetch data like store procedure
try...
SqlCommand cmd = new SqlCommand("select * from [dbo].[LISTSTAFF]()", cnn);
cmd.CommandType = CommandType.Text;
gridview.DataSource = cmd.ExecuteReader();
hope this will help you.