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.
Q 2-- write a programme to find the given no. is prime or not?
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.
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Dim Mynumber As Integer
- Dim isEven As Boolean
- Mynumber = Val(TextBox1.Text)
- If Mynumber Mod 2 = 0 Then
- isEven = True
- MsgBox("The number " & " " & Mynumber & " is an even number")
- Else
- MsgBox(Mynumber & " " & "is an Odd number")
- End If
- End Sub
- 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
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.SqlClientPublic Class Form1Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickDim 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 SubEnd Class
Comments
Post a Comment