Programming for Everybody: 2022

Fill dataGridview from Excel's sheet depend on comBobox in C# with source code

 




using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Data.OleDb;

namespace excel_import

{

    public partial class Form6 : Form

    {

        public Form6()

        {

            InitializeComponent();

        }

        OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Ace.Oledb.12.0;" +

            @"Data Source=F:names2.Xlsx;Extended Properties = 'Excel 8.0;HDR=Yes'");

        private void Form6_Load(object sender, EventArgs e)

        {

            conn.Open();

            comboBox1.DataSource = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

            comboBox1.DisplayMember = "Table_Name";

            comboBox1.SelectedIndex = -1;

            comboBox1.SelectedText = "Select sheet";

            conn.Close();

        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

        {


        }


        private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)

        {

            OleDbCommand cmd = new OleDbCommand("Select * From [" + comboBox1.GetItemText(comboBox1.SelectedItem) + "]", conn);

            OleDbDataAdapter da = new OleDbDataAdapter(cmd);

            DataTable dt = new DataTable();

            da.Fill(dt);

            dataGridView1.DataSource = dt;

        }

    }

}


With source code Excel macro VBA tutorial| insert update delete in sheet excel using entry form

 



Private Sub CommandButton1_Click()

Dim sheet_v As Worksheet

Set sheet_v = ThisWorkbook.Sheets("sheet1")

Dim lrow_v As Long

lrow_v = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row

sheet_v.Range("A" & lrow_v + 1).Value = TextBox1.Value

sheet_v.Range("B" & lrow_v + 1).Value = TextBox2.Value

sheet_v.Range("C" & lrow_v + 1).Value = TextBox3.Value

sheet_v.Range("D" & lrow_v + 1).Value = TextBox4.Value

TextBox1.Value = ""

TextBox2.Value = ""

TextBox3.Value = ""

TextBox4.Value = ""

'MsgBox ("Data inserted successfully")

Call load_data

End Sub

Sub load_data()

Dim sheet_v As Worksheet

Set sheet_v = ThisWorkbook.Sheets("sheet1")

Dim lrow_v As Long

lrow_v = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row

sheet_v.Range("A" & lrow_v + 1).Value = TextBox1.Value

With ListBox1

.ColumnCount = 4

.ColumnHeads = True

.ColumnWidths = "70,90,90,90"

.RowSource = "sheet1!A2:D" & lrow_v

End With

End Sub


Private Sub CommandButton2_Click()

Dim lrow_v As Long

lrow_v = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row

Dim i As Long

For i = 2 To lrow_v

If Sheets("sheet1").Cells(i, 1).Value = id.Text Then

Sheets("sheet1").Cells(i, 1).Value = TextBox1.Text

Sheets("sheet1").Cells(i, 2).Value = TextBox2.Text

Sheets("sheet1").Cells(i, 3).Value = TextBox3.Text

Sheets("sheet1").Cells(i, 4).Value = TextBox4.Text

End If

Next i

End Sub


Private Sub CommandButton3_Click()

Dim lrow_v As Long

lrow_v = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row

Dim i As Long

For i = 2 To lrow_v

If Sheets("sheet1").Cells(i, 1).Value = id.Text Then

Rows(i).Delete

End If

Next i

TextBox1.Value = ""

TextBox2.Value = ""

TextBox3.Value = ""

TextBox4.Value = ""

id.Value = ""

End Sub


Private Sub ListBox1_Click()


End Sub


Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)

TextBox1.Value = ListBox1.Column(0)

TextBox2.Value = ListBox1.Column(1)

TextBox3.Value = ListBox1.Column(2)

TextBox4.Value = ListBox1.Column(3)

id.Value = ListBox1.Column(0)

End Sub


Private Sub UserForm_Click()


End Sub


Private Sub UserForm_Initialize()

Call load_data

End Sub


Important videos c#

Programming C# : Connect SQL server database with Visual Studio C#  with source code

https://youtu.be/8_W0pEZawYg

c# tutorial for beginners - insert update delete search in sql server database and print (With code)

https://youtu.be/mSW7FFzdQMA

Programming C#: insert, update and  delete  data in datagridview without using database

https://youtu.be/Uoz4NS15lj8

C#: update all data from datagridview to database at once

https://youtu.be/_yansR7fE7o

c# tutorial for beginners: How to store images  update and delete in sql database

https://youtu.be/AwvrH8QK7DM

C# Tutorial : Retrieve data from Sql server database

https://youtu.be/lR_Ic_u8e90

C# Tutorial import data from Excel to SQL server

https://youtu.be/NUE-Sgq6SOg

c# tutorial for beginners: Add Row Total To DataGridView Footer

https://youtu.be/aL7wIUSfb-Q

c# tutorial for beginners: Insert Only Checked rows from datagridview into SQL database

https://youtu.be/XZEXOfcue8w

C#: Get data in datagridview from two tables using inner join and (Left - right - full) outer join.

https://youtu.be/eZQmFkL7Ie8

c# tutorial for beginners: Send data from datagridview to crystalreport without database in C#

https://youtu.be/TX-r99vKM3o

c# tutorial for beginners: Print data from dataGridView In C#

https://youtu.be/7EtsdSB72p4

c# tutorial for beginners: How to connect Microsoft access database to C#

https://youtu.be/wRzOkkptwVc

c# tutorial for beginners: How to get the sum of checked RadioButton Values

https://youtu.be/-lURcHfH7Qo

How to get selected text and selected value of comboBox in C#

https://youtu.be/18Z9v7jkERw

c# tutorial for beginners: Retrieve data from access database and navigation buttons

https://youtu.be/pQlhabZPZSU

c# tutorial for beginners: Delete row from datagridview and sql server database at once in C#

https://youtu.be/SUFVWlJGt8E

c# tutorial for beginners - How to search multiple columns access database using one textBox in C#

https://youtu.be/Ni4IDcufTos

C# Tutorial - message box exit application

https://youtu.be/jVNGCBQlXlI

c# tutorial for beginners - How to get the selected items in the combobox and show them in a listbox

https://youtu.be/q-FDCz7u1DM

C#: datagridview change cell backcolor based on value

https://youtu.be/lVYvDJn-r9E

c# tutorial for beginners - How to make validation on textbox in c#

https://youtu.be/wcme5LmppU8

How to fill combobox from sql server database and get selected value from a combobox in c#

https://youtu.be/ti_zVHaRT_U

How to search data in datagridview in c# without using database

https://youtu.be/iuDRFIXvKcg

c# tutorial for beginners - Insert all data of a dataGridView to access database at once  with  code

https://youtu.be/l0pPXY5shjw

Programming C#:  How to prevent duplicate data when insert to datagridview

https://youtu.be/6Ea1XGTjFnI

C#: Search for value in DataGridView in a column(without database)

https://youtu.be/1pkcAnZNAPE

C#:Show and hide characters password

https://youtu.be/mEwFk-46l7s

Programming in C # tutorial:  Login Form in C # with SQL Server with source code

https://youtu.be/NyTnhCsYlGQ 

C# tutorial| connect with access database with password to retrieve data into textboxes -source code



using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Data.OleDb;


namespace connect_access_with_password

{

    public partial class Form2 : Form

    {

        public Form2()

        {

            InitializeComponent();

        }


        private void button1_Click(object sender, EventArgs e)

        {

            OleDbConnection oledconn = new OleDbConnection(@"Provider=Microsoft.Ace.Oledb.12.0;Data Source=F:\ddd.accdb;Jet Oledb:Database Password=123456");

            oledconn.Open();

            OleDbCommand olecomm = new OleDbCommand("Select firstname,lastname,points from table1 Where id = @id", oledconn);

            olecomm.Parameters.AddWithValue("id", textBox1.Text);

            OleDbDataReader oledreader;

            oledreader = olecomm.ExecuteReader();

            if (oledreader.Read())

            {

                textBox2.Text = oledreader["firstname"].ToString();

                textBox3.Text = oledreader["lastname"].ToString();

                textBox4.Text = oledreader["points"].ToString();

            }

            else

            {

                textBox2.Text = "";

                textBox3.Text = "";

                textBox4.Text = "";

                MessageBox.Show("No data found");

                


            }

            oledconn.Close();

        }

    }

Filter Crystal Report using combobox in VB.net with source code

 




Imports System.Data.SqlClient

Public Class Form1

    Dim conn As New SqlConnection("Data Source=.;Initial Catalog=sports;Integrated Security=true")

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        combo1()

        report1()

    End Sub

    Private Sub combo1()

        conn.Open()

        Dim strsql As New SqlCommand("Select Distinct sport From Table1", conn)

        Dim myreader As SqlDataReader = strsql.ExecuteReader

        ComboBox1.Items.Clear()

        While myreader.Read()

            ComboBox1.Items.Add(myreader("sport"))

        End While

        conn.Close()

    End Sub

    Private Sub report1()

        Dim cmd As New SqlCommand("Select * From Table1 Where sport Like '%" + ComboBox1.Text + "%'", conn)

        Dim da As New SqlDataAdapter(cmd)

        Dim dt As New DataTable

        da.Fill(dt)

        Dim cr_report As New CrystalReport1

        cr_report.SetDataSource(dt)

        CrystalReportViewer1.ReportSource = cr_report

        CrystalReportViewer1.Zoom(90%)

        CrystalReportViewer1.Refresh()

    End Sub


    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged

        report1()

    End Sub

End Class

c# tutorial for beginners - hotel management system project



 using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Data.SqlClient;

namespace Book__room

{

    public partial class Form3 : Form

    {

        public Form3()

        {

            InitializeComponent();

        }

        SqlConnection conn = new SqlConnection("Data Source=.;Initial catalog=hotel;Integrated Security=true");

        string strsql;

        SqlCommand cmd;

        private void button1_Click(object sender, EventArgs e)

        {

            strsql = "insert into table1(room,startdate,enddate)Values(@room1,@start1,@end1)";

            cmd = new SqlCommand(strsql, conn);

            cmd.Parameters.AddWithValue("room1", comboBox1.Text);

            cmd.Parameters.AddWithValue("start1", dateTimePicker1.Value.ToString());

            cmd.Parameters.AddWithValue("end1", dateTimePicker2.Value.ToString());

            conn.Open();

            cmd.ExecuteNonQuery();

            conn.Close();

            MessageBox.Show("The room reserved successfully");

        }


        private void button2_Click(object sender, EventArgs e)

        {

            conn.Open();

            strsql = "Select * from table1 Where room=@room1 " +

                "And ((@start1 >= startdate And @end1 <= enddate)" +

                "Or (@start1 <= startdate And @end1 >= enddate)" +

                "or (@start1 > startdate And @end1 > enddate And @start1<enddate)" +

                "or (@start1 < startdate And @end1 < enddate And @end1>startdate))";

            cmd = new SqlCommand(strsql, conn);

            cmd.Parameters.AddWithValue("room1", comboBox1.Text);

            cmd.Parameters.AddWithValue("start1", dateTimePicker1.Value.ToString());

            cmd.Parameters.AddWithValue("end1", dateTimePicker2.Value.ToString());

            SqlDataReader myreader = cmd.ExecuteReader();

            if (myreader.Read())

            {

                MessageBox.Show("The room reserved From: " + myreader.GetValue(2).ToString() + " To: " + myreader.GetValue(3).ToString());

            }

            else

            {

                MessageBox.Show("The room not reserved before");

            }

            conn.Close();

        }

    }

}


VB.net: count and Insert Checked rows from DataGridView to sql Database with source code

 


Imports System.Data.SqlClient

Public Class Form2

    Dim conn As New SqlConnection("Data Source=.;Initial Catalog=students;Integrated Security=true")

    Dim countcheck As Integer

    Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged

        If e.RowIndex < 0 Then Return

        Dim ischecked As Boolean = CBool(DataGridView1.Rows(e.RowIndex).Cells(0).Value)

        If ischecked Then

            countcheck += 1

        Else

            countcheck -= 1

        End If

        Label1.Text = countcheck

    End Sub


    Private Sub DataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged

        If DataGridView1.IsCurrentCellDirty Then

            DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)

        End If

    End Sub


    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load

               Dim cmd As New SqlCommand("Select * from table1", conn)

        Dim da As New SqlDataAdapter(cmd)

        Dim dt As New DataTable

        da.Fill(dt)

        DataGridView1.DataSource = dt

               Dim checkboxcol As New DataGridViewCheckBoxColumn

        checkboxcol.Width = 90

        checkboxcol.Name = "checkboxcol"

        checkboxcol.HeaderText = "Select student"

        DataGridView1.Columns.Insert(0, checkboxcol)

        DataGridView1.AllowUserToAddRows = False

    End Sub


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

        If countcheck > 0 Then

            For Each row As DataGridViewRow In DataGridView1.Rows

                Dim select1 As Boolean = Convert.ToBoolean(row.Cells("checkboxcol").Value)

                If select1 Then

                    Dim cmd2 As New SqlCommand("Insert Into table7(firstname,lastname,marks)Values(@firstname,@lastname,@marks)", conn)

                    cmd2.Parameters.AddWithValue("firstname", row.Cells("firstname").Value)

                    cmd2.Parameters.AddWithValue("lastname", row.Cells("lastname").Value)

                    cmd2.Parameters.AddWithValue("marks", row.Cells("marks").Value)

                    conn.Open()

                    cmd2.ExecuteNonQuery()

                    conn.Close()

                End If

            Next

            MessageBox.Show("Data Inserted successfully")

        Else

            MessageBox.Show("Please select students")

        End If

    End Sub

End Class

Retrieve data from sql server database in asp.net using VB.net





Imports System.Data.SqlClient

Partial Class _Default

    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim constring As String = ConfigurationManager.ConnectionStrings("sportsconn").ConnectionString

        Dim conn As New SqlConnection(constring)

        conn.Open()

        Dim cmd As New SqlCommand("Select name1,age,sport,points,Format(date_reg, 'dd/MM/yyyy') As date_reg From table1 Where id=@id", conn)

        cmd.Parameters.AddWithValue("id", TextBox1.Text)

        Dim myreader As SqlDataReader

        myreader = cmd.ExecuteReader()

        If myreader.Read() Then

            Label7.Visible = False

            TextBox2.Text = myreader("name1").ToString()

            TextBox3.Text = myreader("age").ToString()

            TextBox4.Text = myreader("sport").ToString()

            TextBox5.Text = myreader("points").ToString()

            If Not (myreader.IsDBNull(myreader.GetOrdinal("date_reg"))) Then

                TextBox6.Text = myreader("date_reg")

            Else

                TextBox6.Text = ""

            End If

        Else

            Label7.Visible = True

            TextBox2.Text = ""

            TextBox3.Text = ""

            TextBox4.Text = ""

            TextBox5.Text = ""

            TextBox6.Text = ""

        End If

conn.close()

    End Sub

End Class 

Code in web config:

 <connectionStrings>

    <add name="sportsconn" connectionString="Data Source=.;Initial Catalog=sports;Integrated Security=true" providerName="System.Data.Sqlclient" />

  </connectionStrings>

Visual Basic.net: insert date and check a date between two dates in access database(system book room in a hotel)



 Imports System.Data.OleDb

Public Class Form6

    Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\dd.mdb")


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

        Dim strsql1 As String

        strsql1 = "insert into table1(room1,startdate1,enddate1)Values(@room,@start1,@end1)"

        Dim cmd1 As New OleDbCommand(strsql1, conn)

        cmd1.Parameters.AddWithValue("@room", OleDbType.VarChar).Value = ComboBox1.Text

        cmd1.Parameters.AddWithValue("@start1", OleDbType.Date).Value = DateTimePicker1.Value.ToString

        cmd1.Parameters.AddWithValue("@end1", OleDbType.Date).Value = DateTimePicker2.Value.ToString


        conn.Open()

        cmd1.ExecuteNonQuery()

        conn.Close()

        MessageBox.Show("The Room reserved successfully")

    End Sub

  

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        conn.Open()

        Dim strsql2 As String

        strsql2 = " Select * from table1 where room1=@room " & _

            " And ((@start1>=startdate1 and @end1<= enddate1)" & _

            " or (@start1<=startdate1 and @end1>= enddate1)" & _

            " or (@start1>startdate1 and @end1> enddate1 and @start1 <enddate1 )" & _

            " or (@start1<startdate1 and @end1< enddate1 and @end1 >startdate1 ))"

        Dim cmd2 As New OleDbCommand(strsql2, conn)

        cmd2.Parameters.AddWithValue("@room", OleDbType.VarChar).Value = ComboBox1.Text

        cmd2.Parameters.AddWithValue("@start1", OleDbType.Date).Value = DateTimePicker1.Value.ToString

        cmd2.Parameters.AddWithValue("@end1", OleDbType.Date).Value = DateTimePicker2.Value.ToString


        Dim myreader As OleDbDataReader = cmd2.ExecuteReader

        If (myreader.Read()) Then

            MessageBox.Show(" The room reserved from: " & (myreader.GetValue("2").ToString & " to: " & (myreader.GetValue("3").ToString)))

        Else

            MessageBox.Show(" The Room not reserved before")

        End If

        conn.Close()


    End Sub

End Class