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
...
Jignesh TrivediPosted Jul 15, 2014, 1:21 AM
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.
Palle TechnologiesPosted Jul 14, 2014, 10:50 AM
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.
Dong Lam TrienPosted Jun 26, 2014, 12:03 AM
sudipta sanyalPosted Jun 23, 2014, 2:50 AM
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 TrienPosted Jun 23, 2014, 2:31 AM
Ranjit PowarPosted Jun 23, 2014, 12:46 AM
Dong Lam TrienPosted Jun 23, 2014, 12:38 AM
Ranjit PowarPosted Jun 22, 2014, 1:32 AM
Dong Lam TrienPosted Jun 21, 2014, 8:09 PM
Ranjit PowarPosted Jun 21, 2014, 10:28 AM
Try this
DataSet ds=new DataSet();
SqlDataAdapter da = new SqlDataAdapter("Select *from LISTSTAFF()",cnn);
da.Fill(ds);
gridview.DataSource =ds.Tables[0];Dong Lam TrienPosted Jun 21, 2014, 4:55 AM
[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 PowarPosted Jun 21, 2014, 12:49 AM
DataAdapter da=new DataAdapter(cmd);
Dong Lam TrienPosted Jun 21, 2014, 12:40 AM
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 PowarPosted Jun 21, 2014, 12:19 AM
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 TrienPosted Jun 21, 2014, 12:05 AM
[CODE]
SqlDataReader dr = cmd.ExecuteReader();
if (dr .HasRows)
{
gridview1.DataSource=dr; // warning error in here
gridview1.databind();
}
[/CODE]
Abhishek KumarPosted Jun 20, 2014, 4:41 AM
SqlDataReader dr = cmd.ExecuteReader();
if (dr .HasRows)
{
}
Hope this helps.
Abhishek
Ranjit PowarPosted Jun 20, 2014, 4:18 AM
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.