Vb.net Some programmes

Q 1 --write a programme to find  the given no. is even or odd?
Q 2-- -write a programme to find  factorial the given no.?
Q 3 -- write a programme to find  the given no.  is prime or not?
Q 4--  creating a connection & show data on data grid view.
Q 5-- write a programme to print 1 to 10 no in a list box .


Q1 Ans.

  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. Dim Mynumber As Integer
  3. Dim isEven As Boolean
  4. Mynumber = Val(TextBox1.Text)
  5. If Mynumber Mod 2 = 0 Then
  6. isEven = True
  7. MsgBox("The number " & " " & Mynumber & " is an even number")
  8. Else
  9. MsgBox(Mynumber & " " & "is an Odd number")
  10. End If
  11. End Sub
  12. End Class




Public Class Form1 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 

Dim i, x, fact As Integer 
x = TextBox1.Text 
fact = 1
For i = x To 1 Step -1 
fact = fact * i 
Next i 
TextBox2.Text = fact 

End Sub

End Class


Q 2--  write a programme to find  the given no.  is prime or not?

        Dim t As Boolean
        i = TextBox1.Text
        t = True
        For j = 2 To (i - 1)
            If i Mod j = 0 Then
                t = False
                Exit For
            End If
        Next j
        If t Then
            MsgBox(i & " is a prime Number")
        Else
            MsgBox(i & " is not a prime Number")
        End If






   Q 4 Ans :    Now we create a database table and insert some values in this table. Table looks like this.

create table emp
(
empid varchar(40),
empname varchar(30) 
)

   Imports System.Data.SqlClient
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connectionString As String = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"
        Dim sql As String = "SELECT * FROM Authors"
        Dim connection As New SqlConnection(connectionString)
        Dim dataadapter As New SqlDataAdapter(sql, connection)
        Dim ds As New DataSet()
        connection.Open()
        dataadapter.Fill(ds, "Authors_table")
        connection.Close()
        DataGridView1.DataSource = ds
        DataGridView1.DataMember = "Authors_table"
    End Sub
End Class


Or  


Imports System.Data.SqlClient
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandles Button1.Click
        Dim str As String = "Data Source=.;uid=sa;pwd=123;database=master"
        Dim con As New SqlConnection(str)
        Dim com As String = "Select empid, empname from Emp"
        Dim Adpt As New SqlDataAdapter(com, con)
        Dim ds As New DataSet()
        Adpt.Fill(ds, "Emp")
        DataGridView1.DataSource = ds.Tables(0)
    End Sub
End Class

Comments

Popular posts from this blog

MS Excel

MS Word - Home Tab

Power point presentation