If GridMerit.Rows.Count > 0 Then
GridMerit.EmptyDataText = "Tiada Kesalahan Disiplin Yang Telah Disahkan"
Else
Dim totalmerit As Integer
Dim mySQLConnection = New SqlConnection()
mySQLConnection.ConnectionString = ConfigurationManager.ConnectionStrings("Default").ConnectionString
mySQLConnection.Open()
Dim cmd As New SqlCommand()
cmd.Connection = mySQLConnection
lblIdPelajar.Text = Request.QueryString("IdPelajar")
cmd.CommandText = "Select Sum(MataMerit) From MataMerit Where IdPelajar=@Idpelajar"
cmd.Parameters.AddWithValue("idpelajar", lblIdPelajar.Text)
totalmerit = Convert.ToInt32(cmd.ExecuteScalar())
lbltotal.Text = totalmerit
End If
i use this code
If GridMerit.Rows.Count > 0 Then
GridMerit.EmptyDataText = "Tiada Kesalahan Disiplin Yang Telah Disahkan"
to check the gridview has a data or not, but this code not working need help
if the user got no data it will see the error message like below..so how to resolve it
Object cannot be cast from DBNull to other types.
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.InvalidCastException: Object cannot be cast from DBNull to other types. Source Error:
Line 55: cmd.CommandText = "Select Sum(MataMerit) From MataMerit Where IdPelajar=@Idpelajar"
Line 56: cmd.Parameters.AddWithValue("idpelajar", lblIdPelajar.Text)
Line 57: totalmerit = Convert.ToInt32(cmd.ExecuteScalar()) Line 58: lbltotal.Text = totalmerit
Line 59: End If
Your error completely shows that the query returns nothing which in turn is NULL, so it can't cast it to toint.
There has to be some problem there in the query or query making. I just can't understand.
lblIdPelajar.Text - does it store the proper text? Did you debug through it?
You want ""Tiada Kesalahan Disiplin Yang Telah Disahkan"" to be displayed when there is no data? And if your query does not return anyhing, ExecuteScalar will get null. You can check if cmd.ExecuteScalar() is null then
GridMerit.EmptyDataText="Tiada Kesalahan Disiplin Yang Telah Disahkan".
Or make a blank datatable and assign the above text to rows(0).column(0) by adding a blank row to that. Then assign that datatable as datasource to your grid.
Regards,
Snigdha
Please Mark as Answer if my reply helped you.
Marked as answer by afastars on Nov 24, 2012 01:56 PM
You must define a parameter token before applying this,here's an example——
Dim totalmerit AsIntegerDim mySQLConnection = New SqlConnection()
mySQLConnection.ConnectionString = ConfigurationManager.ConnectionStrings("Default").ConnectionString
mySQLConnection.Open()
Dim cmd AsNew SqlCommand()
cmd.Connection = mySQLConnection
cmd.CommandText = "Select Sum(MataMerit) From MataMerit Where IdPelajar=@IdPelajar"cmd.Parameters.AddWithValue("@IdPelajar",Your Real Value Here) lbltotal.Text = cmd.ExecuteScalar().ToString()
afastars
Member
52 Points
221 Posts
Re: Combine 2 SQL Query
Nov 23, 2012 11:39 AM|LINK
i think you misunderstood what i want...this is static right...?
the system administrator able to select which user Id they want to see, so the code shoukd not be like that right..?
afastars
Member
52 Points
221 Posts
Re: Combine 2 SQL Query
Nov 23, 2012 12:09 PM|LINK
hi sargamlucy
i got it this is the full code..please see below
If GridMerit.Rows.Count > 0 Then GridMerit.EmptyDataText = "Tiada Kesalahan Disiplin Yang Telah Disahkan" Else Dim totalmerit As Integer Dim mySQLConnection = New SqlConnection() mySQLConnection.ConnectionString = ConfigurationManager.ConnectionStrings("Default").ConnectionString mySQLConnection.Open() Dim cmd As New SqlCommand() cmd.Connection = mySQLConnection lblIdPelajar.Text = Request.QueryString("IdPelajar") cmd.CommandText = "Select Sum(MataMerit) From MataMerit Where IdPelajar=@Idpelajar" cmd.Parameters.AddWithValue("idpelajar", lblIdPelajar.Text) totalmerit = Convert.ToInt32(cmd.ExecuteScalar()) lbltotal.Text = totalmerit End Ifi use this code
If GridMerit.Rows.Count > 0 Then
GridMerit.EmptyDataText = "Tiada Kesalahan Disiplin Yang Telah Disahkan"
to check the gridview has a data or not, but this code not working need help
if the user got no data it will see the error message like below..so how to resolve it
Object cannot be cast from DBNull to other types.
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.InvalidCastException: Object cannot be cast from DBNull to other types.
Source Error:
Line 55: cmd.CommandText = "Select Sum(MataMerit) From MataMerit Where IdPelajar=@Idpelajar" Line 56: cmd.Parameters.AddWithValue("idpelajar", lblIdPelajar.Text) Line 57: totalmerit = Convert.ToInt32(cmd.ExecuteScalar()) Line 58: lbltotal.Text = totalmerit Line 59: End Ifsargamlucy
Member
559 Points
164 Posts
Re: Combine 2 SQL Query
Nov 23, 2012 12:22 PM|LINK
Your error completely shows that the query returns nothing which in turn is NULL, so it can't cast it to toint.
There has to be some problem there in the query or query making. I just can't understand.
lblIdPelajar.Text - does it store the proper text? Did you debug through it?
You want ""Tiada Kesalahan Disiplin Yang Telah Disahkan"" to be displayed when there is no data? And if your query does not return anyhing, ExecuteScalar will get null. You can check if cmd.ExecuteScalar() is null then GridMerit.EmptyDataText = "Tiada Kesalahan Disiplin Yang Telah Disahkan".
Or make a blank datatable and assign the above text to rows(0).column(0) by adding a blank row to that. Then assign that datatable as datasource to your grid.
Regards,
Snigdha
Please Mark as Answer if my reply helped you.
afastars
Member
52 Points
221 Posts
Re: Combine 2 SQL Query
Nov 23, 2012 12:45 PM|LINK
i'm using this code below
If cmd.ExecuteScalar() Is Nothing Then
GridMerit.EmptyDataText =
"Tiada Maklumat Merit"
Else
totalmerit = Convert.ToInt32(cmd.ExecuteScalar())
lbltotal.Text = totalmerit
End If
but still no luck so far
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Combine 2 SQL Query
Nov 24, 2012 12:30 AM|LINK
Hi,
You must define a parameter token before applying this,here's an example——
afastars
Member
52 Points
221 Posts
Re: Combine 2 SQL Query
Nov 24, 2012 01:55 PM|LINK
hai decker...
again you resolve my isssues, after try it so many time..now you give me a shine
thanks again my expertise.....