Friday, 14 October 2016

insert update delete find and fill textboxes c# 2010

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;//added for sqlserver database

namespace project_RETAIL
{
    public partial class Supplier : Form
    {
        public Supplier()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {//insert code
            string date = textBox1.Text.ToString();
            string supid = textBox2.Text.ToString();
            string supname = textBox3.Text.ToString();
            string supadd = textBox4.Text.ToString();
            string supcompany = textBox5.Text.ToString();
            string contactno = textBox6.Text.ToString();
            string connectionstring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=e:\project-retail\dbRetail.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            SqlConnection con = new SqlConnection(connectionstring);
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into tblsupplier values(@date, @supid,@supname,@supadd,@supcompany,@supcontactno)", con);
            cmd.Parameters.AddWithValue("@date", date);
            cmd.Parameters.AddWithValue("@supid", supid);
            cmd.Parameters.AddWithValue("@supname", supname);
            cmd.Parameters.AddWithValue("@supadd", supadd);
            cmd.Parameters.AddWithValue("@supcompany", supcompany);
            cmd.Parameters.AddWithValue("@contactno",contactno);
            con.Close();
            MessageBox.Show("saved successfully");
        }

        private void button4_Click(object sender, EventArgs e)
        {//find and fill textboxes
            string date=textBox1.Text.ToString();
            string supid=textBox2.Text.ToString();
            string supname=textBox3.Text.ToString();
            string supadd=textBox4.Text.ToString();
            string supcompany=textBox5.Text.ToString();
            string contactno=textBox6.Text.ToString();
            string connectionstring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=e:\project-retail\dbRetail.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            SqlConnection con = new SqlConnection(connectionstring);
            SqlCommand cmd=new SqlCommand("select * from tblsupplier where supId='"+supid+"'",con);
                con.Open();
            SqlDataReader dr1=cmd.ExecuteReader();
            if(dr1.Read())
           
              {

              textBox1.Text=dr1.GetValue(0).ToString();
        textBox2.Text=dr1.GetValue(1).ToString();
        textBox3.Text=dr1.GetValue(2).ToString();
        textBox4.Text=dr1.GetValue(3).ToString();
        textBox5.Text=dr1.GetValue(4).ToString();
        textBox6.Text=dr1.GetValue(5).ToString();

               }
            con.Close();
         
        }

        private void button3_Click(object sender, EventArgs e)
        {//delete code
            string date=textBox1.Text.ToString();
            string supid=textBox2.Text.ToString();
            string supname=textBox3.Text.ToString();
            string supadd=textBox4.Text.ToString();
            string supcompany=textBox5.Text.ToString();
            string contactno=textBox6.Text.ToString();
            string connectionstring=@"Data Source=.\SQLEXPRESS;AttachDbFilename=e:\project-retail\dbRetail.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            SqlConnection con=new SqlConnection(connectionstring);
            con.Open();
            SqlCommand cmd=new SqlCommand("delete from tblsupplier where supId='"+supid+"'",con);
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Deleted successfully");

        }

        private void button2_Click(object sender, EventArgs e)
        {//update code
            string date = textBox1.Text.ToString();
            string supid = textBox2.Text.ToString();
            string supname = textBox3.Text.ToString();
            string supadd = textBox4.Text.ToString();
            string supcompany = textBox5.Text.ToString();
            string contactno = textBox6.Text.ToString();
            string connectionstring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=e:\project-retail\dbRetail.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            SqlConnection con = new SqlConnection(connectionstring);
            con.Open();
            SqlCommand cmd = new SqlCommand("update tblsupplier set supDate='" + date + "',supId='" + supid + "',supName='" + supname + "',supAddress='" + supadd + "',supCompany='" + supcompany + "',supContact='" + contactno + "'where supid='" + supid + "'", con);
            cmd.Parameters.AddWithValue("@supDate", date);
            cmd.Parameters.AddWithValue("@supId", supid);
            cmd.Parameters.AddWithValue("@supName", supname);
            cmd.Parameters.AddWithValue("@supAddress", supadd);
            cmd.Parameters.AddWithValue("@supCompany", supcompany);
            cmd.Parameters.AddWithValue("@Contactno", contactno);
            con.Close();
            MessageBox.Show("updates successfully");
        }

       

        }




        }
   




Thursday, 29 September 2016

crystal report
2 major errors handle them like this

drag crystalreportviewer control on form1
if not shown in toolbox
click properties -from solution explorer
1)change Target  framework .net framework client profile to .net framework 4
also 
change in 

2)app.config file
in startup tag
Add uselegacyv2 runtime activation policy=true in <startup> tag like given below




<?xml version="1.0"?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="PRG25_STUD_CRYSTAL_DEMO.Properties.Settings.dbStudentConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbStudent.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
    </connectionStrings>
<startup useLegacyV2RuntimeActivationPolicy="true"><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>



code to show crystal report from 1 button click


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;
using CrystalDecisions.CrystalReports.Engine;
namespace PRG25_STUD_CRYSTAL_DEMO
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbStudent.mdf;Integrated Security=True;User Instance=True");
            SqlCommand com;
            SqlDataAdapter a = new SqlDataAdapter();
            DataSet d = new DataSet();
            a = new SqlDataAdapter("select * from tbl_student", con);
            a.Fill(d);
            ReportDocument doc = new ReportDocument();

            doc.Load(@"D:\PRG25_STUD_CRYSTAL_DEMO\PRG25_STUD_CRYSTAL_DEMO\CrystalReport1.rpt");

            doc.SetDataSource(d.Tables[0]);
            crystalReportViewer1.ReportSource = doc; 
        }
    }
}


 .......................................
Add DataSet -add Adapter then choose .mdf file
add Crystal report 
Left side' Database fields-Database expert-project DataSet-Ado.net DataSet-datasetname tblstudent click > sign to move,

drag database fields in PageDetail area of crystal report then it will take header and its title to its header area automatically.