Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How should i use the variable declared in the the vb code in sql query to update the table.
i want to insert the total into score table.

here's the code:
VB
Dim count As Integer
        Dim count1 As Integer
        Dim count2 As Integer
        Dim count3 As Integer
        Dim total As Integer
       
        Dim mycmd As New SqlCommand()
        Dim mycmd1 As New SqlCommand()
        Dim mycmd2 As New SqlCommand()
        Dim mycon As New SqlConnection("Data Source=testvm;Initial Catalog=ULSLLIVE;Integrated Security=True;")
        mycon.Open()
        mycmd.Connection = mycon
        mycmd1.Connection = mycon
        mycmd.CommandText = "select count(*) from answer where a=1 ;"
        mycmd1.CommandText = "select count(*) from  answer where a=0;"
        count = CType(mycmd.ExecuteScalar(), Integer)
        count1 = CType(mycmd1.ExecuteScalar(), Integer)
        count2 = 2 * count
        count3 = 1 * count1
        total = count2 - count3
        
        mycmd2.Connection = mycon
        mycmd2.CommandText = "update score set marks=('" & total & "') where username=('" & Form2.Label3.Text & "');"
        mycmd.ExecuteNonQuery()
        mycon.Close()
Posted
Comments
The Doer 21-Jun-13 3:38am    
What is the problem here? You are done with it, any errors?

1 solution

Try this:
C#
mycmd2.CommandText = "update score set marks=@TOT where username=@UN";
mycmd2.Parameters.AddWithValue("@TOT", total);
mycmd2.Parameters.AddWithValue("@UN", Form2.Label3.Text);
mycmd.ExecuteNonQuery()


However, if Form2 is a class then you can't access labels like that, as they would have to be static, and controls don't work like that. If this code is part of the Form2 class, then:
C#
mycmd2.Parameters.AddWithValue("@UN", Label3.Text);
If it is a different class, then you need to look at one of these:
Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]
Exactly which one will depend on the relationship between the forms.
 
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