Reputation: 1854
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,
Upvotes: 1
Views: 807
Reputation: 3705
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
Upvotes: 1
Reputation: 3515
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
Upvotes: 0