Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have issue that I could not execute MSsql stored procedure in asp.net

It is working fine and gives the return value when executing in Mssql server console.
My code in asp.net page
Dim strConnString As String = ConfigurationManager.ConnectionStrings("ConnectString").ConnectionString
Dim con As New SqlConnection(strConnString)
Dim cmd As New SqlCommand()
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "usp_Z_SO_Details"
cmd.Parameters.AddWithValue("@DATEFROM", Me.txtDateFrom.Text)
cmd.Parameters.AddWithValue("@DATETO", Me.txtDateTo.Text)
cmd.Parameters.AddWithValue("@CATEGORY ", Me.cboCategory.SelectedValue)
cmd.Connection = con
con.Open()
cmd.ExecuteReader()
con.Close()
con.Dispose()


My stored Procedure


INSERT INTO [dbo].[RPT_SO_Details](SoNo, SDate, SDay, DealerCode, Dealer, DistCode, DistName, Area, 
		                                   RouteCode, [Route], CatCode, Category, ZoneCode, ZoneDesc, NoStand, 
										   Nocopies)
		SELECT SoNo, SDate, SDay, DealerCode, Dealer, DistCode, DistName, Area, 
		                                   RouteCode, [Route], CatCode, Category, ZoneCode, ZoneDesc, NoStand, 
										   Nocopies
		FROM SUNMedia.dbo.StandingOrder
		
		WHERE SDATE >= @DATEFROM AND SDATE <= @DATETO AND CATEGORY = @CATEGORY 
		ORDER BY CATEGORY

		SELECT * FROM [dbo].[RPT_SO_Details] ORDER BY ROUTECODE,sono



Pls advice me where i did wrong

Thank you

Maideen

What I have tried:

Dim strConnString As String = ConfigurationManager.ConnectionStrings("ConnectString").ConnectionString
        Dim con As New SqlConnection(strConnString)
        Dim cmd As New SqlCommand()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "usp_Z_SO_Details"
        cmd.Parameters.AddWithValue("@DATEFROM", Me.txtDateFrom.Text)
        cmd.Parameters.AddWithValue("@DATETO", Me.txtDateTo.Text)
        cmd.Parameters.AddWithValue("@CATEGORY ", Me.cboCategory.SelectedValue)
        cmd.Connection = con
        con.Open()
        cmd.ExecuteReader()
        con.Close()
        con.Dispose()
Posted
Updated 9-Jul-17 23:46pm

Start by doing three things:
1) Don't use ExecuteReader - you aren't using the results at all, because you discard the SqlDataReader that it returns. Why does you SP code do the final select at all, given that this is an INSERT INTO SELECT statement - your code would return all the rows...
2) Check your user inputs: Sending text dates to SQL is a bad idea. Use DateTime.TryParse[^] to convert teh user intoputs to DateTiume values - reporting problems to the user - and pass the DateTime values directly to SQL as parameters. Passing strings means that SQL has to "guess" what format the date is in, and it doesn't always share the same locale as your user.
3) I know this is damn obvious, but check the column datatype of your SDATE column - if it's a string rather than a DATE, DATETIME, or DATETIME2 then that will cause major problems with date comparisons. You'd be surprised how many people think that NVARCHAR is a good datatype to store dates in and it always gives them real problems later on.
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900