473,586 Members | 2,555 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Input string was not in a correct format.

15 New Member
hi

i m finding following error on the code that i wants to use to get all record from table via store procedure with paging. the error is :
Input string was not in a correct format.

after a hectic struggle still i dont know
1--who can i solve it and
2--where should i have to place the function GETDATA . who is it possible to keep all the functions in a separate file and to call that file in required page.

here is the code of whole page where i m getting error::

Expand|Select|Wrap|Line Numbers
  1. Imports system.data
  2. Imports system.data.sqlclient
  3. Partial Class Department_detailDept
  4.     Inherits System.Web.UI.Page
  5.     Private Sub GetData()
  6.  
  7.         ' Declare local variables. 
  8.         Dim totalClients As Integer
  9.         Dim totalPages As Integer
  10.         Dim connClients As New SqlConnection()
  11.         Dim cmdClients As New SqlCommand()
  12.         Dim parmClients As SqlParameter
  13.         Dim drClients As SqlDataReader
  14.         Dim dtClients As New DataTable()
  15.         Dim drClient As DataRow
  16.  
  17.  
  18.  
  19.         ' Define the DataTable. 
  20.         dtClients.Columns.Add("depart_id", System.Type.[GetType]("System.Int32"))
  21.         dtClients.Columns.Add("Department_name", System.Type.[GetType]("System.String"))
  22.  
  23.         ' Create a connection to the database. 
  24.         Try
  25.  
  26.             ' Connect using the connection string in the web.config. 
  27.             connClients.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("DBcon").ConnectionString
  28.             connClients.Open()
  29.  
  30.             ' Configure the command object. 
  31.             cmdClients.CommandText = "phealthMISDepartDetail"
  32.             cmdClients.CommandType = CommandType.StoredProcedure
  33.             cmdClients.Connection = connClients
  34.  
  35.             ' Create the command object parameters. 
  36.             ' Creat the current page parameter. 
  37.             parmClients = New SqlParameter("@CurrentPage", SqlDbType.TinyInt)
  38.             parmClients.Direction = ParameterDirection.Input
  39.             parmClients.Value = dgClients.CurrentPageIndex
  40.             cmdClients.Parameters.Add(parmClients)
  41.  
  42.             ' Create the page size parameter. 
  43.             parmClients = New SqlParameter("@PageSize", SqlDbType.TinyInt)
  44.             parmClients.Direction = ParameterDirection.Input
  45.             parmClients.Value = 5
  46.             cmdClients.Parameters.Add(parmClients)
  47.  
  48.             ' Create the total rows parameter. 
  49.             parmClients = New SqlParameter("@TotalRecords", SqlDbType.Int)
  50.             parmClients.Direction = ParameterDirection.Output
  51.             cmdClients.Parameters.Add(parmClients)
  52.  
  53.             ' Process the command. 
  54.             ' It should return to us the client rows into a DataReader. 
  55.             ' There will also be an output parameter of the total number of clients in the db. 
  56.             ' We need to close the DataReader prior to retrieving the output parameter. 
  57.             drClients = cmdClients.ExecuteReader()
  58.  
  59.             ' Iterate through the DataReader. 
  60.             While drClients.Read()
  61.  
  62.                 ' Create a new DataRow to populate. 
  63.                 drClient = dtClients.NewRow()
  64.  
  65.                 ' Populate the DataRow. 
  66.                 drClient("depart_id") = drClients("depart_id")
  67.                 drClient("Department_name") = drClients("Department_name")
  68.  
  69.  
  70.                 ' Add the DataRow. 
  71.                 dtClients.Rows.Add(drClient)
  72.             End While
  73.  
  74.             ' Close the DataReader. 
  75.             drClients.Close()
  76.  
  77.             ' Retrieve the output parameter. 
  78.             totalClients = CInt(cmdClients.Parameters("@TotalRecords").Value)
  79.  
  80.             ' Bind the DataGrid. 
  81.             dgClients.DataSource = dtClients
  82.             dgClients.DataBind()
  83.  
  84.             ' Configure the footer. 
  85.             lblCurrentPage.Text = dgClients.CurrentPageIndex.ToString()
  86.             If (totalClients Mod 5) = 0 Then
  87.                 totalPages = totalClients / 5
  88.             Else
  89.  
  90.                 totalPages = (totalClients / 5) + 1
  91.             End If
  92.             lblTotalPages.Text = totalPages.ToString()
  93.         Catch ex As Exception
  94.  
  95.             ' Nothing here for now. 
  96.  
  97.         Finally
  98.  
  99.  
  100.             ' Close and dispose of the database connection. 
  101.             cmdClients.Dispose()
  102.             connClients.Close()
  103.             connClients.Dispose()
  104.         End Try
  105.  
  106.     End Sub
  107.  
  108.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  109.  
  110.         ' Is this an initial page load? 
  111.         If Not Page.IsPostBack Then
  112.             'Dim totalPages As Integer
  113.  
  114.  
  115.             ' Store a default value to the current page index on the initial load of the page. 
  116.             dgClients.CurrentPageIndex = 1
  117.  
  118.             ' Initially disable the previous link. 
  119.             lbtnPrevious.Enabled = False
  120.         End If
  121.  
  122.         ' Get the data for the DataGrid. 
  123.         GetData()
  124.  
  125.         ' Get the total pages. 
  126.  
  127.         Dim totalPages As Integer = Integer.Parse(lblTotalPages.Text.ToString())
  128.  
  129.         ' Determine if there is more than 1 page to display. 
  130.         If totalPages > 1 Then
  131.  
  132.             lbtnNext.Enabled = True
  133.         Else
  134.  
  135.             lbtnNext.Enabled = False
  136.         End If
  137.     End Sub
  138.  
  139.     Protected Sub PrePage(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtnPrevious.Click
  140.  
  141.         ' Declare local data members. 
  142.         Dim currentPage As Integer = Integer.Parse(lblCurrentPage.Text.ToString())
  143.         Dim totalPages As Integer = Integer.Parse(lblTotalPages.Text.ToString())
  144.  
  145.         ' Decrement the current page index. 
  146.         If currentPage > 1 Then
  147.  
  148.             dgClients.CurrentPageIndex -= 1
  149.  
  150.             ' Get the data for the DataGrid. 
  151.             GetData()
  152.  
  153.             ' Should we disable the previous link? 
  154.             If dgClients.CurrentPageIndex = 1 Then
  155.  
  156.                 lbtnPrevious.Enabled = False
  157.             End If
  158.  
  159.             ' Should we enable the next link? 
  160.             If dgClients.CurrentPageIndex < totalPages Then
  161.  
  162.                 lbtnNext.Enabled = True
  163.             End If
  164.         End If
  165.     End Sub
  166.  
  167.     Protected Sub NextPage(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtnNext.Click
  168.         Dim currentPage As Integer = Integer.Parse(lblCurrentPage.Text.ToString())
  169.         Dim totalPages As Integer = Integer.Parse(lblTotalPages.Text.ToString())
  170.  
  171.         ' Decrement the current page index. 
  172.         If currentPage > 1 Then
  173.  
  174.             dgClients.CurrentPageIndex -= 1
  175.  
  176.             ' Get the data for the DataGrid. 
  177.             GetData()
  178.  
  179.             ' Should we disable the previous link? 
  180.             If dgClients.CurrentPageIndex = 1 Then
  181.  
  182.                 lbtnPrevious.Enabled = False
  183.             End If
  184.  
  185.             ' Should we enable the next link? 
  186.             If dgClients.CurrentPageIndex < totalPages Then
  187.  
  188.                 lbtnNext.Enabled = True
  189.             End If
  190.         End If
  191.     End Sub
  192.  
  193. End Class
  194.  

store procedure is as follows:
Expand|Select|Wrap|Line Numbers
  1. ALTER Procedure dbo.phealthMISDepartDetail 
  2.  
  3. -- Declare parameters.
  4. @CurrentPage As tinyint,
  5. @PageSize As tinyint,
  6. @TotalRecords As int OUTPUT
  7.  
  8.  
  9. As
  10.  
  11. -- Turn off count return.
  12. Set NoCount On
  13.  
  14. -- Declare variables.
  15. Declare @FirstRec int
  16. Declare @LastRec int
  17.  
  18. -- Initialize variables.
  19. Set @FirstRec = (@CurrentPage - 1) * @PageSize
  20. Set @LastRec = (@CurrentPage * @PageSize + 1)
  21.  
  22. -- Create a temp table to hold the current page of data
  23. -- Add an ID column to count the records
  24. Create Table #TempTable
  25. (
  26. depart_id int IDENTITY PRIMARY KEY,
  27. Department_name varchar(50)
  28. )
  29.  
  30. --Fill the temp table with the reminders
  31. Insert Into #TempTable 
  32. (
  33. Department_name
  34. )
  35. Select
  36. Department_name As Name
  37. From 
  38. Department
  39.  
  40.  
  41. --Select one page of data based on the record numbers above
  42. Select 
  43. depart_id,
  44. Department_name
  45. From 
  46. #TempTable As Department
  47. Where 
  48. depart_id > @FirstRec 
  49. And depart_id < @LastRec
  50.  
  51. --Return the total number of records available as an output parameter
  52. Select
  53. @TotalRecords = Count(*)
  54. From 
  55. Department
  56.  
kindly experts let me out of this problem as soon as possible!!!!
Sep 20 '07 #1
9 3958
Plater
7,872 Recognized Expert Expert
Well I see a number of things wrong.
First off, it appears that you are trying to assign a value to your datareader FROM your datatable (instead of other way around)

Secondly, you're doing a LOT more work then you need to.

This code:
Expand|Select|Wrap|Line Numbers
  1.         ' Define the DataTable. 
  2.         dtClients.Columns.Add("depart_id", System.Type.[GetType]("System.Int32"))
  3.         dtClients.Columns.Add("Department_name", System.Type.[GetType]("System.String"))
  4.  
  5.  
  6.             ' Process the command. 
  7.             ' It should return to us the client rows into a DataReader. 
  8.             ' There will also be an output parameter of the total number of clients in the db. 
  9.             ' We need to close the DataReader prior to retrieving the output parameter. 
  10.             drClients = cmdClients.ExecuteReader()
  11.  
  12.             ' Iterate through the DataReader. 
  13.             While drClients.Read()
  14.  
  15.                 ' Create a new DataRow to populate. 
  16.                 drClient = dtClients.NewRow()
  17.  
  18.                 ' Populate the DataRow. 
  19.                 drClient("depart_id") = drClients("depart_id")
  20.                 drClient("Department_name") = drClients("Department_name")
  21.  
  22.  
  23.                 ' Add the DataRow. 
  24.                 dtClients.Rows.Add(drClient)
  25.             End While
  26.  
  27.             ' Close the DataReader. 
  28.             drClients.Close()
  29.  
Can be turned into:
Expand|Select|Wrap|Line Numbers
  1. Dim mya = new SqlDataAdapter(cmdClients);
  2. mya.Fill(dtClients );
  3.  
Sep 20 '07 #2
seep
15 New Member
what steps i have to take to do it from the scrap, am i going in a wrong way???
Sep 20 '07 #3
Frinavale
9,735 Recognized Expert Moderator Expert
what steps i have to take to do it from the scrap, am i going in a wrong way???
Hi Seep,

Could you please put your code within [code] tags.
For instance if you want to post VB.Net code you'd type:
Expand|Select|Wrap|Line Numbers
  1.  'My Vb.Net code goes here
  2.  
This will make it a lot easier to read your posts.

Thanks a lot!

-Frinny
Sep 20 '07 #4
seep
15 New Member
pls help me i m struck in this problem and find no way to out. kindly help m e with in coding. waiting for ur response thanks for all in advance
Sep 23 '07 #5
krris
10 New Member
hi

First check ur all Data. u r trying to convert String into Int,Decimal or else.
and Pass String null or blank thats why this error comes.

check Data comes correct.
Sep 24 '07 #6
seep
15 New Member
really hoof..
still i may not b able to correct this. is their any one who can provide me code for this??
needs an urgent response!!!
Sep 25 '07 #7
Frinavale
9,735 Recognized Expert Moderator Expert
really hoof..
still i may not b able to correct this. is their any one who can provide me code for this??
needs an urgent response!!!
You've posted a lot of code.
Could you please post the line of code that the error is occurring on...
Have you checked your connection string?
Sep 25 '07 #8
seep
15 New Member
i found error at this:
Expand|Select|Wrap|Line Numbers
  1. Dim totalPages As Integer = Integer.Parse(lblTotalPages.Text.ToString
at line 127 of the above posted code. so, pls guide me.
Sep 26 '07 #9
Frinavale
9,735 Recognized Expert Moderator Expert
i found error at this:
Expand|Select|Wrap|Line Numbers
  1. Dim totalPages As Integer = Integer.Parse(lblTotalPages.Text.ToString
at line 127 of the above posted code. so, pls guide me.
Try excluding the ToString.
So change
Expand|Select|Wrap|Line Numbers
  1. Dim totalPages As Integer = Integer.Parse(lblTotalPages.Text.ToString())
to
Expand|Select|Wrap|Line Numbers
  1. Dim totalPages As Integer = Integer.Parse(lblTotalPages.Text.ToString())
Sep 26 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
21083
by: Jim | last post by:
im using asp.net, C# to enter data into a table in sql server...however im getting this error: Input string was not in a correct format. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. ...
0
768
by: lianfe_ravago | last post by:
Input string was not in a correct format. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.FormatException: Input string was not in a correct format. Source Error: ...
5
2504
by: blackg | last post by:
Input string not in correct format -------------------------------------------------------------------------------- I am trying to view a picture from a table. I am getting this error Input string not in the correct format. Input string was not in a correct format. Description: An unhandled exception occurred during the execution of the...
1
1839
by: amitbadgi | last post by:
Welcome back amitbadgi | Logout | Faq Knowledge Discovery Keys COMPUTER PROGRAMMING, DATA MINING, STATISTICS, ARTIFICIAL INTELLIGENCE * Settings * Photos * Lists * MVPs * Forums * Blogs
1
2842
by: amitbadgi | last post by:
I am gettign this error, while migration an app to asp.net Exception Details: System.FormatException: Input string was not in a correct format. Source Error: Line 19: Dim enddate = request.QueryString("enddate") Line 20: Line 21: if cint(eventid) = "0" then
0
2538
by: hudhuhandhu | last post by:
have got an error which says Input string was not in a correct format. as follows.. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.FormatException: Input string was...
0
11961
by: Anonieko | last post by:
Are there any javascript codes there? Answer: Yes On the PageLoad event call InitialClientControsl as follows /// <summary> /// This will add client-side event handlers for most of the form controls so
0
3160
by: sehguh | last post by:
Hiya Folks, I am Currently using windows xp. Also using Visual Web Developer 2005 and Microsoft Sql server 2005. The main page consists of an aspx page and a master page. The page also consists of a label control(hidden when run in browser). Also an Sql data source control connected to database tables for a photo album. Also label web...
1
4516
by: sehguh | last post by:
Hello folks I have recently been studying a book called "sams teach yourself asp.net 2.0 in24 hours by scott mitchell. I have reached page 614 but when i tried to run an asp page called Photoadmin/default.aspx i got the following message and info. Input string was not in a correct format. Description: An unhandled exception occurred...
1
4747
by: differentsri | last post by:
THIS IS AN ASP.NET 1.1 APPLICATION IAM TRYING TO UPDATE THE FIELD BUT I AM NOT ABLE TO UPDATE IT? CAN U TELL THE REASON ? IT IS GIVING THE FOLLOWING ERROR BELOW I HAVE ALSO GIVEN THE CODE OF THE PROGRAM PLEASE HELP ME Server Error in '/WebApplication1' Application....
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.