Programming for Everybody

How to attach database mdf file in sql server and fix an error occurred when attaching the database

 How to attach database mdf file in sql server and fix an error occurred when attaching the databases, Click the hyperlink in the Message column for detail.






Visual Basic.net: get duplicate records in SQL Server[ with source code ]

 


Imports System.Data.SqlClient
Public Class Form1
    Dim conn As New SqlConnection("Data Source=.;Initial Catalog=Names;Integrated Security=True")
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If conn.State = ConnectionState.Closed Then
            conn.Open()

        End If

        Dim cmd1 As New SqlCommand("select * from table1 where name In (Select name from table1 Group by name having (Count(*)>1)) order by name", conn)

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

        conn.Close()






    End Sub
End Class

C# : Create Login form in C# step by step [ 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;
using System.Data.SqlClient;


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

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Data source=.; initial catalog=users;integrated security=true");

            SqlCommand cmd = new SqlCommand("select * from login1 where user1=@user1 and pass1=@pass1", conn);

            cmd.Parameters.Add("@user1", SqlDbType.VarChar).Value = textBox1.Text;

            cmd.Parameters.Add("@pass1", SqlDbType.VarChar).Value = textBox2.Text;


            SqlDataAdapter adapter1 = new SqlDataAdapter(cmd);

            DataTable table = new DataTable();

            adapter1.Fill(table);

            if (table.Rows.Count == 1)
            {

                Form2 gg = new Form2();

                gg.Show();

                this.Hide();

            }
            else
            {
                MessageBox.Show("Error username or password");


            }



        }

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

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

            if (checkBox1.Checked == true)
            {
                textBox2.UseSystemPasswordChar = false;
            }
            else
            {

                textBox2.UseSystemPasswordChar = true;
            }



        }
    }

    }

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();

        }
    }
}


Find Duplicates From a Table in SQL Server

 



Select id,name,tel from table1

where name In (Select name from table1 Group by name having (Count(*)>1))
ORDER BY Name

Visual Basic.net: Retrieve data from SQL Server database and navigation Buttons[ with source code ]

 



Imports System.Data.SqlClient


Public Class Form2
    Dim table As New DataTable

    Dim index As Integer


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

    End Sub
    Public Sub loadData(ByVal position As Integer)

        Dim con1 As New SqlConnection("Data Source=prog-pc;Initial Catalog=d2;Integrated Security=True")
        Dim com1 As New SqlCommand("select id,firstname,lastname,telephone,sum from table1", con1)
       
        Dim adapter As New SqlDataAdapter(com1)

        adapter.Fill(table)


        TextBox1.Text = table.Rows(position)(0).ToString
        TextBox2.Text = table.Rows(position)(1).ToString
        TextBox3.Text = table.Rows(position)(2).ToString
        TextBox4.Text = table.Rows(position)(3).ToString
        TextBox5.Text = table.Rows(position)(4).ToString









    End Sub

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

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        index += 1
        loadData(index)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        index -= 1
        loadData(index)
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        index = table.Rows.Count - 1
        loadData(index)


    End Sub
End Class