c#


Cannot find table 0 for ExecuteDataSet in c#


I am getting exception for
DataTable ProbRead = SQLData.ExecuteDataSet("SELECT description FROM chk_maintenance_probremark where subheaderid = " + lblrefno.Text.ToString() + " and Type='Problem'").Tables[0];
System.IndexOutOfRangeException: Cannot find table 0.
seems your sql query does not have a result
try this
var probRead = SQLData.ExecuteDataSet("SELECT description FROM chk_maintenance_probremark where subheaderid = " + lblrefno.Text.ToString() + " and Type='Problem'");
if(probRead.Tables.Count > 0)
{
var myTable = probRead.Tables[0];
// do some stuff
}
Make the query readable:
//TODO: get rid of hardcoding, but paramterize the query
//DONE: ToString() is redundant in lblrefno.Text
string sql =
$#"select Description
from Chk_Maintenance_ProbRemark
where SubHeaderId = '{lblrefno.Text}' and
Type = 'Problem'";
and you'll see the problem clearly: lblrefno.Text.ToString() should be wrapped with '.
Do not forget to test if you have any data returned:
var probRead = SQLData.ExecuteDataSet(sql);
if (probRead.Tables.Count > 0) {
// It's safe now to call probRead.Tables[0]
...
}

Related Links

Access and overwrite an xml node (C#)
Persist cookies across multiple requests
How to handle switch user in c#?
Limit the maximum number of characters in a double
Hosting ASP.net app on somee - database update from C# app
I have a deserialize Json C# class now i want to fetch information from it
Lightswitch - C# LINQ Preprocess Query
Passing arguments to a .NET exe file with _spawnv
Backgroundwork DoWork process not running after installing application
How to make one column of sql as Hyperlink in C#.Net
How to display my list<T> elements in a text box
How to print a panel in WinFormc#?
Input arguments and password in cmd.exe in winform C#
c# home works can't solve [closed]
generic class for linq / lambda expression
Access Denied Exception with Message Dialog

Categories

HOME
javafx
xamarin.forms
phantomjs
reference
squeak
ms-access-2010
uml-designer
cdn
restheart
annyang
mbed
console-application
greensock
filehelpers
alert
openoffice-basic
minimagick
vala
infinite-loop
quality-center
gsuite
scrapy-spider
sqldependency
csproj
wordpress-rest-api
flickr
fastadapter
n1ql
client-server
word-wrap
lex
origami
armadillo
ssdp
mixed-models
directadmin
superclass
result
chrome-custom-tabs
ocpjp
hierarchical-clustering
stanford-nlp-server
google-cloud-shell
jcifs
fluent-nhibernate-mapping
webmatrix
scanf
turbolinks
hdr
mate-desktop
particle-system
db2-zos
django-celery
supervised-learning
3d-rendering
paypal-subscriptions
factorization
wizard
firefox-os
nslocalizedstring
syncano
winforms-interop
web-standards
targetprocess
fileinputstream
firebase-tools
strcpy
jad
cl.exe
uiblureffect
fedena
mind-manager
cocos2d-x-2.x
selenium-grid2
amzi-prolog
sendy
image-preloader
boofcv
netbeans-plugins
adabas
code-complete
mobile-robots
maven-webstart-plugin
cocotron
generator-expression
animationdrawable
designated-initializer
moss2007-security
cryptolicensing
http-response-codes
text-manipulation
database-agnostic
morfik
data-formats
retrospectiva
opengl-to-opengles
content-delivery-network
stackless
ironpython-studio

Resources

Encrypt Message