Showing posts with label line. Show all posts
Showing posts with label line. Show all posts

Monday, March 19, 2012

datareader wont read first line of DB

hi there, i have some code which cycles through my table in my DB and fills some textboxes. For some reason if there are 5 lines it will only get 4, yet strangly i used the code somewhere else for the same kind of thing and it reads all the lines, and anyone help?

int a = 0;int b = 0;

//This is the sql statement.

string sqlMultiL ="SELECT * FROM tbl_stock_part_multi_location WHERE stock_ID =" +Convert.ToInt32(Request.QueryString["qsStockID"]);// + ") AND EXISTS (SELECT * FROM tbl_stock_part_multi_location WHERE stock_ID =" + Convert.ToInt32(Request.QueryString["qsStockID"]) + ")";

//This creates a sql command which executes the sql statement.

SqlCommand sqlCmdMultiL =new SqlCommand(sqlMultiL, myConnMultiL);

myConnMultiL.Open();

SqlDataReader drMultiL = sqlCmdMultiL.ExecuteReader();

//This reads the first result from the sqlReader

while (drMultiL.Read())

{

try

{

((TextBox)Panel2.FindControl("txtDesc_L" + a.ToString())).Text =Convert.ToString(drMultiL["description"]);

for (b = 1; b <= 5; b++)

{

((TextBox)Panel2.FindControl("txtQty" + b.ToString() +"_L" + a.ToString())).Text =Convert.ToString(drMultiL["qty" + b.ToString()]);

}// end of for b loop

}

catch (Exception exMultiL)

{

lblError.Text = lblError +Convert.ToString(exMultiL);

}

//increase the counter

a++;

// you can add a break if you only want 5 rows returned:

if (a >= 5)break;

}

also i dont know if anyone could answer this is if the value in my DB is not = to one in the DDL how can i handle that so it dosnt throw an error and cause my code to stop?

Thanks in advance

Jez

Change to

for (b = 0; b <= 5; b++) //You need to start from the 0

{

a++; //Here you missed

((TextBox)Panel2.FindControl("txtQty" + a.ToString() +"_L" + a.ToString())).Text =Convert.ToString(drMultiL["qty" + a.ToString()]); //Need to change to a too

}// end of for b loop

|||

hi gave that a go and still no luck :( i have tried changing it to 0 before by my controls start from 1 etc. As for changing the controls all to a.ToString() i think thats wrong because it would just do txtQty1_L1, txtQty2_L2 etc instead of going from 1 - 5 and then to the next L

DataReader Output skipping first line

I am using the following code to query a DB and output an Excel Spreadsheet. For some reason, the first line is always missing.

Any help would be greatly appreciated.


============================================

1reader = cmd.ExecuteReader()2If Not reader.Read()Then3 Return False4Else5 For i = 0To reader.FieldCount - 16 strLine += reader.GetName(i).ToString & Chr(9)7Next8 objStreamWriter.WriteLine(strLine)9 strLine =""10With reader11While .Read12For x = 0To .FieldCount - 113 strLine = strLine & reader.GetValue(x) & Chr(9)14Next15 objStreamWriter.WriteLine(strLine)16 strLine =""17End While18 End With19End If

line 2 of you code is wasting the first row of data by performing a reader.Read that you ignore.

change line 2 to:

If Not reader.HasRows()Then
|||

Perfect!

Thank you!!!!

Sunday, March 11, 2012

Dataflow task -> Error Handling

Hi,
In terms of data flow tasks, when say we load text files into databases.

Is it possible to have it in a way so that if a certain record (line in the text file) fails to load due to watever reason, it gets written to another table, but the rest of the records still get loaded?

I try to do so and end up with the whole data flow task failing and it stalls at the record that had the error and doesn't seem to continue forward.

I just used the red arrow (on failure) and put that to another SQL destination object. But yeah that didnt work.

If someone has a better way of doing so, would be awesome if you can share that.

Cheers

You have to set the 'Access mode' property of 'OleDB DEstination' OpenRowset.It should not be fastload.Then set ErrorOuput as Redirect Row. Now you can get your erroneous rows redirected.

This should do.

|||yup...i didnt know about the accessmode property. that did it. thanks
|||Dear all,

I face the same problem but I didn't find the 'Access mode' property of 'OleDB DEstination' that contain OpenRowset its only contain Tabel and View with Fastload. Where I can find that property ?
And what if the error occured in Flat File Source ? can we still using the same solution ? Thanks in advance.

Best Regards,

Hery|||Dear all,

Please help me on this.

Thanks in advance.

Dataflow task -> Error Handling

Hi,
In terms of data flow tasks, when say we load text files into databases.

Is it possible to have it in a way so that if a certain record (line in the text file) fails to load due to watever reason, it gets written to another table, but the rest of the records still get loaded?

I try to do so and end up with the whole data flow task failing and it stalls at the record that had the error and doesn't seem to continue forward.

I just used the red arrow (on failure) and put that to another SQL destination object. But yeah that didnt work.

If someone has a better way of doing so, would be awesome if you can share that.

Cheers

You have to set the 'Access mode' property of 'OleDB DEstination' OpenRowset.It should not be fastload.Then set ErrorOuput as Redirect Row. Now you can get your erroneous rows redirected.

This should do.

|||yup...i didnt know about the accessmode property. that did it. thanks
|||Dear all,

I face the same problem but I didn't find the 'Access mode' property of 'OleDB DEstination' that contain OpenRowset its only contain Tabel and View with Fastload. Where I can find that property ?
And what if the error occured in Flat File Source ? can we still using the same solution ? Thanks in advance.

Best Regards,

Hery|||Dear all,

Please help me on this.

Thanks in advance.

Thursday, March 8, 2012

Dataflow task -> Error Handling

Hi,
In terms of data flow tasks, when say we load text files into databases.

Is it possible to have it in a way so that if a certain record (line in the text file) fails to load due to watever reason, it gets written to another table, but the rest of the records still get loaded?

I try to do so and end up with the whole data flow task failing and it stalls at the record that had the error and doesn't seem to continue forward.

I just used the red arrow (on failure) and put that to another SQL destination object. But yeah that didnt work.

If someone has a better way of doing so, would be awesome if you can share that.

Cheers

You have to set the 'Access mode' property of 'OleDB DEstination' OpenRowset.It should not be fastload.Then set ErrorOuput as Redirect Row. Now you can get your erroneous rows redirected.

This should do.

|||yup...i didnt know about the accessmode property. that did it. thanks
|||Dear all,

I face the same problem but I didn't find the 'Access mode' property of 'OleDB DEstination' that contain OpenRowset its only contain Tabel and View with Fastload. Where I can find that property ?
And what if the error occured in Flat File Source ? can we still using the same solution ? Thanks in advance.

Best Regards,

Hery|||Dear all,

Please help me on this.

Thanks in advance.

Tuesday, February 14, 2012

database use syntax help

I have database called 'test1' and when i use the code below im getting this error
Server: Msg 170, Level 15, State 1, Line 9
Line 11: Incorrect syntax near '@.dbName'.

plz help, here is the code

DECLARE @.dbName varchar(50)
DECLARE @.index int

SET @.index = 1
SET @.dbName ='test'

set @.dbName = @.dbName + CAST(@.index as varchar)
use @.dbNameu cannot do that. if u want to select rows from table1 of test1 database
select * from test1..table1

or u can use dynamic sql like

DECLARE @.dbName varchar(50)
DECLARE @.index int

SET @.index = 1
SET @.dbName ='test'

set @.dbName = @.dbName + CAST(@.index as varchar)
exec ('select * from ' + @.dbName + '..table1')

remember dynamic SQLs r having security issues if not managed carefully|||thanks for replying. but i want to use 'use' syntax not 'exec'

this works fine
use test1
select * from table1

but when i use this i get the error
use 'test' + '1'
select * from table1|||Everything Upsalen has said is correct. His code is equivalent to what you requrest.

If you really must use USE then:
DECLARE @.dbName varchar(50)
DECLARE @.index int

SET @.index = 1
SET @.dbName ='test'

set @.dbName = @.dbName + CAST(@.index as varchar)
exec ('USE ' + @.dbname + ' GO select * from table1')|||The reason i want to use 'USE' is i have around 20 database (test1,test2,test3,...test20) with same schema, everytime when i make update or fix scripts i have to run it 20 times. now i want to use loop and run it once for all the databases.

here is what the code looks like...

DECLARE @.dbName varchar(50)
DECLARE @.index int
DECLARE @.NUM_OF_DB int

SET @.NUM_OF_DB = 20

SET @.dbName = 'test'
set @.index = 1

while @.index <= @.NUM_OF_DB
begin

set @.dbName = dbName + CAST(@.index as varchar)
USE @.dbName

-- paste here the scripts (script is thousands of lines long)

set @.index = @.index + 1
SET @.dbName = 'test'
end
or if you know easy way to do this plz ...|||yes, i know easy way to do this

if you have 20 databases which are essentially identical in structure (they would have to be if you can run the same thousand-line script on their tables), then just combine them into one database and vwalah, your USE problem goes away

:)|||Rudy's suggestion. My Code. Upsalen's code. Your code does not, and cannot, work. Unless perhaps you write something in .NET or similar and execute it from there. You cannot do what you want to do from QA\ SSMS.|||yes, i know easy way to do this

if you have 20 databases which are essentially identical in structure (they would have to be if you can run the same thousand-line script on their tables), then just combine them into one database and vwalah, your USE problem goes away

:)
c'mon......i have good reason not to do that|||okay, you have good reason

how about this: write 20 scripts, each with a different USE, each calling the same common script

:cool:|||You may be able to write some code using ADO\ ADO.NET - execute your script, looping through the various databases, changing the database at each pass.

I suspect that Rudy's point is that if you have 20 databases of all identical schemas then perhaps your design is flawed... Perhaps it isn't. But perhaps it is.|||You may be able to write some code using ADO\ ADO.NET - execute your script, looping through the various databases, changing the database at each pass.

I suspect that Rudy's point is that if you have 20 databases of all identical schemas then perhaps your design is flawed... Perhaps it isn't. But perhaps it is.

about design, 20 of them are same database. i just use them for training, testing,etc which means at the end it is one database.

where can i find code for ADO\ ADO.NET.|||were can i find code for ADO\ ADO.NET.You'll have to write it I am afraid. I don't have time to come up with any right now.|||I suspect that Rudy's point is that if you have 20 databases of all identical schemas then perhaps your design is flawed... Perhaps it isn't. But perhaps it is.well, yes, that was my point in post #6

but then, after having been assured that there is good reason for 20 databases, i put forth in post #9 a modest programming suggestion (not requiring ADD or whatever that was)

this suggestion, had it been undertaken, would have solved the problem elegantly

and about half an hour ago

:)|||well, yes, that was my point in post #6

but then, after having been assured that there is good reason for 20 databases, i put forth in post #9 a modest programming suggestion (not requiring ADD or whatever that was)

this suggestion, had it been undertaken, would have solved the problem elegantly

and about half an hour ago

:)
okey im just looking for easy way. how can i call common script?
to explain about db design i have one database copied 20 times for different purpose.|||ADO - ActiveX Data Objects
not to be confused with Data Access Objects which are totally different ;)

Yes - I missed the calling thingy - probably easier than ADO.|||another simple way to do this is to write a .bat file that calls osql.exe or sqlcmd.exe 20 times with the same script, each time connecting to a different database.|||another simple way to do this is to write a .bat file that calls osql.exe or sqlcmd.exe 20 times with the same script, each time connecting to a different database.
nice one i will give it a try|||Just wondering, does

DECLARE @.dbName varchar(50)
SET @.dbName = 'test'
USE @.dbName

Work?
If so, I might have a suggestion :p|||Just wondering, does

DECLARE @.dbName varchar(50)
SET @.dbName = 'test'
USE @.dbName

Work?
If so, I might have a suggestion :pNope :)|||another simple way to do this is to write a .bat file that calls osql.exe or sqlcmd.exe 20 times with the same script, each time connecting to a different database.
as your suggestion i write simple batch file and its working great just the way i want it. thank you men you saved me lot of time.
here is what the batch file looks like

SET YOGI_HOME=%cd%
SET /P IN_PUT_FILE=Script file name:
SET OUT_PUT_FILE=out.txt
SET NUM_DB=20
SET i=1
.
.
.

:Loop
Echo Executing script file %IN_PUT_FILE% on test%i%. . .
cd C:\Program Files\Microsoft SQL Server\80\Tools\Binn\
isqlw -s [Local] -d [test%i%] -E -u [sa] -p [password] -i [%YOGI_HOME%\%IN_PUT_FILE%] -o [%YOGI_HOME%\%OUT_PUT_FILE%]
.
.
.