Showing posts with label somebody. Show all posts
Showing posts with label somebody. Show all posts

Tuesday, March 27, 2012

Filling SQLDataReader with stored procedure recordset

Hi All,

I'm hoping somebody can help me with this as it is driving me mad. I've created a stored procedure which returns Employee information recordset when the windows username is passed to it as a parameter. I want to then store this information in Session variables so content can be filtered depending on employee status, but when I execute my code no records are returned. I know for a fact that the stored procedure works because I bound it sqldatasource and displayed results from it in a Datalist and tested it in sql server. My code is as follows can anybody see any problems with it, in runs through fine with but when I try a read a field from the datareader in says there is no data to read.

Dim CurrentUserAs String, Pos1As Int16, EmployeeIdAs Int32Dim cnAs New System.Data.SqlClient.SqlConnectionDim paramAs New System.Data.SqlClient.SqlParameterDim readerAs System.Data.SqlClient.SqlDataReaderDim cmdAs New System.Data.SqlClient.SqlCommand CurrentUser =CStr(User.Identity.Name) Pos1 = InStr(CurrentUser,"\") + 1 CurrentUser = Mid(CurrentUser, Pos1) Session("User") = CurrentUser Session("CID") =Nothing cn.ConnectionString ="Data Source=LAPTOP-4\SQLEXPRESS;Initial Catalog=SCMdb;Integrated Security=True" cn.Open() cmd.Connection = cn cmd.CommandText ="CurrentUser" cmd.CommandType = CommandType.StoredProcedure param = cmd.CreateParameter param.ParameterName ="@.UserName" param.SqlDbType = SqlDbType.VarChar param.Value = CurrentUser cmd.Parameters.Add(param) reader = cmd.ExecuteReader(CommandBehavior.CloseConnection) EmployeeId = reader.Item("EmployeeID") reader.Close()

Any help would be much appricated this is driving me mad.

Thank You

Shaft

To read items from reader,

reader = cmd.ExecuteReader (CommandBehavior.CloseConnection):

while (reader.Read())
{
int _employeeId = (int) reader["EmployeeID"];
}

Thanks

|||

Hi there,

If you are just reading a single value of the stored procedure I will advice you to use OUTPUT parameters to achieve this.Datareader will also do the job, but you leave yourself more vulnerable with connection not closing properly and also with overhead of creating a reader object.Just a thought.. what you are doing is also correct and will work

|||

e_screw:

To read items from reader,

reader = cmd.ExecuteReader (CommandBehavior.CloseConnection):

while (reader.Read())
{
int _employeeId = (int) reader["EmployeeID"];
}

Thanks

I've tried that all ready but it still says there is no data.

Any other ideas anyone?

|||

The actual error is "Invalid attempt to read when no data is present." so I'm thinking the Command Object hasn't pulled any data which leads me think there is a problem with the way I've declared the parameter because it is running the stored procedure just not retrieving any data.

|||

Sorry may mistake it did work I'd just forgot to comment out the statment that was wrong.

Here's the code that e-screw posted but converted to vb .net as I needed it

Do While reader.Read EmployeeId = reader("EmployeeID")Loop
Cheers E-Screw

Friday, February 24, 2012

File of LOG

Somebody knows as removing the log file, because the same this occupying a
space.Frank,
Are you talking about transaction log, .ldf file?
--
Dinesh.
SQL Server FAQ at
http://www.tkdinesh.com
"Frank Dulk" <fdulk@.bol.com.br> wrote in message
news:O2ThWl$aDHA.2476@.tk2msftngp13.phx.gbl...
>
>
> Somebody knows as removing the log file, because the same this occupying a
> space.
>|||Yes, because the same already this with 700M
"Dinesh.T.K" <tkdinesh@.nospam.mail.tkdinesh.com> wrote in message
news:um3qcr$aDHA.2436@.TK2MSFTNGP12.phx.gbl...
> Frank,
> Are you talking about transaction log, .ldf file?
> --
> Dinesh.
> SQL Server FAQ at
> http://www.tkdinesh.com
> "Frank Dulk" <fdulk@.bol.com.br> wrote in message
> news:O2ThWl$aDHA.2476@.tk2msftngp13.phx.gbl...
> >
> >
> >
> >
> >
> > Somebody knows as removing the log file, because the same this occupying
a
> > space.
> >
> >
>|||Frank,
You shouldnt remove the transaction log file.Instead take measures to
control its growth. There is no automatic shrinking inbuilt unless you
specify.If you care about log backups, then schedule a sql job which will
take log backups at regular intervals.If you dont care, then check the
option "truncate log on checkpoint" on database properties(sql7), "simple"
recovery model(sql2000) and BACKUP LOG <databasename> WITH TRUNCATE_ONLY
.These actions would only truncate ( or empty ) the log so that no further
space is extracted unless needed.In order to shrink (or reduce the physical
size of the file ), refer:
--SQL7
INF: How to Shrink the SQL Server 7.0 Transaction Log
http://support.microsoft.com/support/kb/Articles/q256/6/50.asp
and/or
www.sqlserverfaq.com and search for "sql7shrinklognowork.txt"
--SQL2000
INF: Shrinking the Transaction Log in SQL Server 2000 with DBCC SHRINKFILE
http://support.microsoft.com/support/kb/Articles/q272/3/18.asp
In case the log doesnt shrink even after DBCC SHRINKFILE, then do a
combination of...
Back up the Database (BACKUP DATABASE dbname...)
Back up the log (BACKUP LOG dbname...)
Truncate the log (BACKUP LOG dbname WITH TRUNCATE_ONLY)
Shrink the file (DBCC SHRINKFILE...)
Dinesh.
SQL Server FAQ at
http://www.tkdinesh.com
"Frank Dulk" <fdulk@.bol.com.br> wrote in message
news:%23mg$wQKbDHA.2368@.TK2MSFTNGP09.phx.gbl...
> Yes, because the same already this with 700M
> "Dinesh.T.K" <tkdinesh@.nospam.mail.tkdinesh.com> wrote in message
> news:um3qcr$aDHA.2436@.TK2MSFTNGP12.phx.gbl...
> > Frank,
> >
> > Are you talking about transaction log, .ldf file?
> >
> > --
> > Dinesh.
> > SQL Server FAQ at
> > http://www.tkdinesh.com
> >
> > "Frank Dulk" <fdulk@.bol.com.br> wrote in message
> > news:O2ThWl$aDHA.2476@.tk2msftngp13.phx.gbl...
> > >
> > >
> > >
> > >
> > >
> > > Somebody knows as removing the log file, because the same this
occupying
> a
> > > space.
> > >
> > >
> >
> >
>