Programming for Everybody: Visual Studio
Showing posts with label Visual Studio. Show all posts
Showing posts with label Visual Studio. Show all posts

Open word document in RichtextBox using VB.net

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim op1 As OpenFileDialog = New OpenFileDialog() With {
            .ValidateNames = True,
            .Multiselect = False,
            .Filter = "Word|*.doc;*.docx"


                }
        If op1.ShowDialog() = DialogResult.OK Then

            Dim readonly1 As Object = True
            Dim visible As Object = True
            Dim save As Object = True
            Dim filename As Object = op1.FileName
            Dim missing As Object = Type.Missing
            Dim template As Object = False
            Dim typedoc As Object = 0

            Dim doc1 As Microsoft.Office.Interop.Word._Document = Nothing
            Dim word1 As Microsoft.Office.Interop.Word._Application = New Microsoft.Office.Interop.Word.Application() With {
              .Visible = False
            }

            doc1 = word1.Documents.Open(filename, readonly1, visible)
            doc1.ActiveWindow.Selection.WholeStory()
            doc1.ActiveWindow.Selection.Copy()
            Dim data1 As IDataObject = Clipboard.GetDataObject
            RichTextBox1.Rtf = data1.GetData(DataFormats.Rtf).ToString
            word1.Quit(missing)


        End If
End Sub

Visual basic.net: search data on datagridview using Textbox value by event TextChanged[ with code ]

 


Imports System.Data.OleDb

Public Class Form1

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

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\ddd.accdb")
      
        If conn.State = ConnectionState.Closed Then
            conn.Open()

        End If


        Dim cmd1 As New OleDbCommand("select id,firstname,lastname,sum from table1 where firstname like '%" + TextBox1.Text + "%'", conn)

        Dim da As New OleDbDataAdapter
        Dim dt As New DataTable

        da.SelectCommand = cmd1
        dt.Clear()
        da.Fill(dt)
        DataGridView1.DataSource = dt

        

        conn.Close()



    End Sub
End Class

Connect SQL server database with Visual Studio 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.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'names1DataSet.Table1' table. You can move, or remove it, as needed.
            this.table1TableAdapter.Fill(this.names1DataSet.Table1);

            


        }

        private void button1_Click(object sender, EventArgs e)
        {
            table1BindingSource.AddNew();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            table1BindingSource.EndEdit();
            table1TableAdapter.Update(names1DataSet.Table1);

        }

        private void button3_Click(object sender, EventArgs e)
        {
            table1BindingSource.RemoveCurrent();
    table1TableAdapter.Update(names1DataSet.Table1);

        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        private void button5_Click(object sender, EventArgs e)
        {
            table1BindingSource.MoveFirst();

        }

        private void button6_Click(object sender, EventArgs e)
        {
            table1BindingSource.MoveLast();

        }

        private void button7_Click(object sender, EventArgs e)
        {
            table1BindingSource.MovePrevious();

        }

        private void button8_Click(object sender, EventArgs e)
        {
            table1BindingSource.MoveNext();

        }
    }
}


How to update All Data From Datagridview To Database In Vb.net [ with source code ]





Imports System.Data
Imports System.Data.SqlClient

Public Class Form2

    Dim conn1 As New SqlConnection("data source=.;initial catalog=users;integrated security=true")


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


        Dim table As New DataTable("table")



        table.Columns.Add("id", Type.GetType("System.Int32"))
        table.Columns.Add("Name", Type.GetType("System.String"))
        table.Columns.Add("Country", Type.GetType("System.String"))
        table.Columns.Add("id_group", Type.GetType("System.Int32"))


        Dim cmd As New SqlCommand("", conn1)

        With cmd

            .Connection = conn1

            .CommandText = "select id,name,country,id_group from table2 where id_group=" & Label1.Text & ""


        End With

        Dim da As New SqlDataAdapter
        da.SelectCommand = cmd

        Dim dt As New DataTable
        dt.Clear()
        da.Fill(dt)
        DataGridView1.DataSource = dt

        DataGridView1.AllowUserToAddRows = False

        DataGridView1.Columns(0).HeaderText = "ID"

        DataGridView1.Columns(1).HeaderText = "Name"


        DataGridView1.Columns(2).HeaderText = "Country"


        DataGridView1.Columns(3).HeaderText = "Id_group"








    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For i As Integer = 0 To DataGridView1.Rows.Count - 1

            Dim cmd4 As New SqlCommand("", conn1)

            cmd4.CommandText = "update table2 set name='" & DataGridView1.Rows(i).Cells(1).Value & "',country='" & DataGridView1.Rows(i).Cells(2).Value & "' where id=" & DataGridView1.Rows(i).Cells(0).Value & ""

            conn1.Open()
            cmd4.ExecuteNonQuery()
            conn1.Close()


        Next
        MessageBox.Show("Rows updated successfully")


    End Sub

    
   

   
   
    Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
        If e.KeyCode = Keys.Right Then


            For i As Integer = 0 To DataGridView1.Rows.Count - 1
                Dim constring5 As String = "Data Source=.;Initial Catalog=users;Integrated Security = true"
                Dim Query5 As String = "select id,name,country,id_group from table2 where id= '" & DataGridView1.Rows(i).Cells(0).Value & "'"
                Dim cn5 As SqlConnection = New SqlConnection(constring5)
                Dim cmd5 As SqlCommand = New SqlCommand(Query5, cn5)


                cn5.Open()

                If True Then

                    Using read5 As SqlDataReader = cmd5.ExecuteReader()

                        While read5.Read()
                            DataGridView1.Rows(i).Cells(1).Value = (read5("Name"))
                            DataGridView1.Rows(i).Cells(2).Value = (read5("Country"))



                        End While
                    End Using
                End If



                cn5.Close()





            Next
        End If
    End Sub
End Class