Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the code I have written on Update button of Gridview . I wanted to check total of weighting factor and I wanted to write the javascript message if total exceeds 1. How can I do this . Not very much familiar with Javascript Embeding in vb code .

VB
Protected Sub UpdateFuncObjDetails(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
    lblwtFactTotalmsg.Visible = False
    Dim Conn As New SqlConnection(strConnString)
    Dim sum As Double = 0.0
    Dim str1 As String = String.Empty
    Dim Comm1 As New SqlCommand

    str1 = "select ISNULL(sum(MBO_Weighting_Factor),0) from tbl_MBO_Objective_Details where MBO_Req_Id is null and MBO_EMP_CODE='" & EMP_NO & "'"
    Comm1 = New SqlCommand(str1, Conn)
    Conn.Open()
    sum = Comm1.ExecuteScalar()
    Conn.Close()
' Here is code I want to check whether weighting factor exceeds 1 if it exceeds one 
' it should display message in javascript that 
' Your weghting factor total exceeds you can not 
'update the record

    Dim LblID As Integer = DirectCast(GridViewMBOFuncObjDetalis.Rows(e.RowIndex).FindControl("lblEditID"), Label).Text
    Dim txtfuncobj As String = DirectCast(GridViewMBOFuncObjDetalis.Rows(e.RowIndex).FindControl("txtfuncobj0"), TextBox).Text
    Dim txtmeasuredvar As String = DirectCast(GridViewMBOFuncObjDetalis.Rows(e.RowIndex).FindControl("txtmeasuredvar0"), TextBox).Text
    Dim txtdeadline As String = DirectCast(GridViewMBOFuncObjDetalis.Rows(e.RowIndex).FindControl("txtdeadline0"), TextBox).Text
    Dim txtweight_fact As String = DirectCast(GridViewMBOFuncObjDetalis.Rows(e.RowIndex).FindControl("txtweight_fact0"), TextBox).Text

    Dim con As New SqlConnection(strConnString)
    Dim cmd As New SqlCommand()
    cmd.CommandType = CommandType.Text
    cmd.CommandText = "update tbl_MBO_Objective_Details set MBO_Objective=@func_obj,MBO_Measured_Var=@mesrd_var,MBO_Target_Date=@tar_dt,MBO_Weighting_Factor=@wt_fact where id=@idnum;" & _
    " select id, MBO_Objective,MBO_Measured_Var,MBO_Target_Date,MBO_Weighting_Factor  from tbl_MBO_Objective_Details where MBO_Req_Id is null and MBO_Obj_Type_Id=1 and MBO_EMP_CODE='" & (EMP_NO) & "'"

    cmd.Parameters.Add("@idnum", SqlDbType.VarChar).Value = LblID
    cmd.Parameters.Add("@func_obj", SqlDbType.VarChar).Value = txtfuncobj
    cmd.Parameters.Add("@mesrd_var", SqlDbType.VarChar).Value = txtmeasuredvar
    cmd.Parameters.Add("@tar_dt", SqlDbType.VarChar).Value = txtdeadline
    cmd.Parameters.Add("@wt_fact", SqlDbType.VarChar).Value = txtweight_fact

    GridViewMBOFuncObjDetalis.EditIndex = -1
    GridViewMBOFuncObjDetalis.DataSource = GetDataMBOFuncObjDetails(cmd)
    GridViewMBOFuncObjDetalis.DataBind()
    BindDataMBOPerObjectives()

   End Sub
Posted
Updated 25-Mar-14 23:16pm
v2

1 solution

Hai

why u go for javascript ? in ur check place (exceeds 1 or not) u use if condition and show ur web message or proceed ur update query.like this

VB
    str1 = "select ISNULL(sum(MBO_Weighting_Factor),0) from tbl_MBO_Objective_Details where MBO_Req_Id is null and MBO_EMP_CODE='" & EMP_NO & "'"
    Comm1 = New SqlCommand(str1, Conn)
    Conn.Open()
    sum = Comm1.ExecuteScalar()
    Conn.Close()

If (sum > 1)Then
showmessage("Your weghting factor total exceeds you can not 
update the record")
Exit Sub 'This Exit Sub will exit the code execution,i mean here after code not execute it goes to end sub of the function

Else

   Dim LblID As Integer = DirectCast(GridViewMBOFuncObjDetalis.Rows(e.RowIndex).FindControl("lblEditID"), Label).Text
    Dim txtfuncobj As String = DirectCast(GridViewMBOFuncObjDetalis.Rows(e.RowIndex).FindControl("txtfuncobj0"), TextBox).Text
    Dim txtmeasuredvar As String = DirectCast(GridViewMBOFuncObjDetalis.Rows(e.RowIndex).FindControl("txtmeasuredvar0"), TextBox).Text
    Dim txtdeadline As String = DirectCast(GridViewMBOFuncObjDetalis.Rows(e.RowIndex).FindControl("txtdeadline0"), TextBox).Text
    Dim txtweight_fact As String = DirectCast(GridViewMBOFuncObjDetalis.Rows(e.RowIndex).FindControl("txtweight_fact0"), TextBox).Text
.................................
...........................
.....................

End If





if u want javascript msg alert try this ,just paste this javascript in html page inside Script tag.I think in sum u get total of weighting factor,if not pls try to get total of weighting factor in sum
JavaScript
function testmsg3(msg) {

 alert(msg);
           return false;

       }


and in below pls see in checking place (exceeds 1 or not)


VB
Protected Sub UpdateFuncObjDetails(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
    lblwtFactTotalmsg.Visible = False
    Dim Conn As New SqlConnection(strConnString)
    Dim sum As Double = 0.0
    Dim str1 As String = String.Empty
    Dim Comm1 As New SqlCommand
 
    str1 = "select ISNULL(sum(MBO_Weighting_Factor),0) from tbl_MBO_Objective_Details where MBO_Req_Id is null and MBO_EMP_CODE='" & EMP_NO & "'"
    Comm1 = New SqlCommand(str1, Conn)
    Conn.Open()
    sum = Comm1.ExecuteScalar()
    Conn.Close()
' Here is code I want to check whether weighting factor exceeds 1 if it exceeds one 
' it should display message in javascript that 
' Your weghting factor total exceeds you can not 
'update the record

if (sum >1) Then

 ScriptManager.RegisterStartupScript(Me.Page, Me.[GetType](), "Alert", "<script type='text/javascript'>testmsg3('Your weghting factor total exceeds you can not 
update the record !');</script>", False)

Else

    Dim LblID As Integer = DirectCast(GridViewMBOFuncObjDetalis.Rows(e.RowIndex).FindControl("lblEditID"), Label).Text
    Dim txtfuncobj As String = DirectCast(GridViewMBOFuncObjDetalis.Rows(e.RowIndex).FindControl("txtfuncobj0"), TextBox).Text
    Dim txtmeasuredvar As String = DirectCast(GridViewMBOFuncObjDetalis.Rows(e.RowIndex).FindControl("txtmeasuredvar0"), TextBox).Text
    Dim txtdeadline As String = DirectCast(GridViewMBOFuncObjDetalis.Rows(e.RowIndex).FindControl("txtdeadline0"), TextBox).Text
    Dim txtweight_fact As String = DirectCast(GridViewMBOFuncObjDetalis.Rows(e.RowIndex).FindControl("txtweight_fact0"), TextBox).Text
 
    Dim con As New SqlConnection(strConnString)
    Dim cmd As New SqlCommand()
    cmd.CommandType = CommandType.Text
    cmd.CommandText = "update tbl_MBO_Objective_Details set MBO_Objective=@func_obj,MBO_Measured_Var=@mesrd_var,MBO_Target_Date=@tar_dt,MBO_Weighting_Factor=@wt_fact where id=@idnum;" & _
    " select id, MBO_Objective,MBO_Measured_Var,MBO_Target_Date,MBO_Weighting_Factor  from tbl_MBO_Objective_Details where MBO_Req_Id is null and MBO_Obj_Type_Id=1 and MBO_EMP_CODE='" & (EMP_NO) & "'"
 
    cmd.Parameters.Add("@idnum", SqlDbType.VarChar).Value = LblID
    cmd.Parameters.Add("@func_obj", SqlDbType.VarChar).Value = txtfuncobj
    cmd.Parameters.Add("@mesrd_var", SqlDbType.VarChar).Value = txtmeasuredvar
    cmd.Parameters.Add("@tar_dt", SqlDbType.VarChar).Value = txtdeadline
    cmd.Parameters.Add("@wt_fact", SqlDbType.VarChar).Value = txtweight_fact
 
    GridViewMBOFuncObjDetalis.EditIndex = -1
    GridViewMBOFuncObjDetalis.DataSource = GetDataMBOFuncObjDetails(cmd)
    GridViewMBOFuncObjDetalis.DataBind()
    BindDataMBOPerObjectives()
  End If
   End Sub
 
Share this answer
 
v4
Comments
Ni!E$H_WAGH 10-Mar-14 2:06am    
Thanks
Ni!E$H_WAGH 10-Mar-14 2:21am    
I tried your solution but it shows error message like
Line: 119672505
Error: Object expected
Aravindba 10-Mar-14 3:15am    
use break point and find exact error line and post here
Ni!E$H_WAGH 10-Mar-14 3:29am    
I Think error is in
ScriptManager.RegisterStartupScript(Me.Page, Me.[GetType](), "Alert", "<script type='text/javascript'>testmsg3('Your weghting factor total exceeds you can not
update the record !');</script>", False)

this part of the code
Aravindba 10-Mar-14 3:34am    
Did u add javascript code in ur html page ?
function testmsg3(msg) {

alert(msg);
return false;

}
if not work try to remove "return false;" form above function and try

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