简体   繁体   中英

cannot find table 0 in c# asp.net

                if (ds.Tables[0].Rows.Count > 0)
                {
                    txtid.Text = ds.Tables[0].Rows[0]["id"].ToString();
                    txttamil.Text = ds.Tables[0].Rows[0]["Tamil"].ToString();
                    txtenglish.Text = ds.Tables[0].Rows[0]["English"].ToString();
                    txtmaths.Text = ds.Tables[0].Rows[0]["Maths"].ToString();
                    txtscience.Text = ds.Tables[0].Rows[0]["Science"].ToString();
                    txtsocialscience.Text = ds.Tables[0].Rows[0]["SocialScience"].ToString();
                }

            }

In the above showed error 'cannot find table 0'.

When i enter invalid id, it need to show "ID does not exist".

May i know, what my mistake in my code?

stored procedure:

ALTER PROCEDURE sp_studentresult
(
    @id int,
    @output varchar(50) output,
    @id_student varchar(50)
)
AS

IF EXISTS (SELECT * FROM student WHERE id=@id_student)
BEGIN
SELECT * from studentresult where id_student=@id
END
ELSE
BEGIN
SET @output='Doesn not EXIST'
End

any help would be highly appreciated. Thanks,

There is no point on complicating your sp with logic.

Why don't you just check if the query has returned any data?

if (ds.Tables[0].Rows.Count > 0)
{
    txtid.Text = ds.Tables[0].Rows[0]["id"].ToString();
    txttamil.Text = ds.Tables[0].Rows[0]["Tamil"].ToString();
    txtenglish.Text = ds.Tables[0].Rows[0]["English"].ToString();
    txtmaths.Text = ds.Tables[0].Rows[0]["Maths"].ToString();
    txtscience.Text = ds.Tables[0].Rows[0]["Science"].ToString();
    txtsocialscience.Text = ds.Tables[0].Rows[0]["SocialScience"].ToString();
}
else
{
    // Whatever you want to do if no row is found
}

And a simpler sp, which will return a empty table if nothing is found

ALTER PROCEDURE sp_studentresult
(
    @id int
)
AS

-- I have removed the extra id column as not sure why you use it
SELECT * from studentresult where id_student=@id

Your SP doesn't return any tabular data in case of a non existing id. Try change it to something like

IF NOT EXISTS (SELECT * FROM student WHERE id=@id_student)
BEGIN
    SET @output='Does not EXIST'
END
SELECT * from studentresult where id_student=@id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM