Dot Net Framework
A)
Write a program in C#.Net to separate the individual characters from a String.
Answer :
using System;
class Program {
static void Main(string[] args) {
string inputString = "Hello,
world!";
char[] characters = inputString.ToCharArray();
Console.WriteLine("Individual
characters from the string: ");
foreach (char c in characters) {
Console.Write(c + " ");
}
Console.ReadLine();
}
}
Output :
B)
Write a VB.NET program to accept the details of customer (CName, Contact No,
Email_id).
Store it into the database with proper validation and display appropriate
message
by using Message box.
Answer :
Imports System.Data.SqlClient
Public Class CustomerForm
' Database connection
string
Dim connectionString As String = "Data
Source=localhost\SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=True"
Private Sub btnSave_Click(sender As Object, e As
EventArgs) Handles btnSave.Click
' Get the
customer details from textboxes
Dim cname As String = txtName.Text.Trim()
Dim
contactNo As String = txtContactNo.Text.Trim()
Dim email As String = txtEmail.Text.Trim()
'
Validate the inputs
If cname = "" Then
MessageBox.Show("Please enter customer name.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
If contactNo = "" Or Not
IsNumeric(contactNo) Then
MessageBox.Show("Please enter valid contact number.",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
If email = "" Or Not
email.Contains("@") Then
MessageBox.Show("Please enter valid email address.",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
' Insert the
customer details into the database
Using
connection As New SqlConnection(connectionString)
Dim query As String = "INSERT
INTO Customers (CName, ContactNo, Email) VALUES (@CName, @ContactNo, @Email)"
Dim
command As New SqlCommand(query,
connection)
command.Parameters.AddWithValue("@CName", cname)
command.Parameters.AddWithValue("@ContactNo",
contactNo)
command.Parameters.AddWithValue("@Email", email)
connection.Open()
Dim
rowsAffected As Integer = command.ExecuteNonQuery()
connection.Close()
If rowsAffected
> 0 Then
MessageBox.Show("Customer
details saved successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Failed
to save customer details.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Using
End Sub
End Class
Output :
No comments:
Post a Comment