Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

how to insert checked items from checkedlistbox into sql server 2008 from c# windows forms

$
0
0

Hi my name is vishal.

For past 2 days i was wondering how to insert checked items from checkedlistbox control in c# windows forms with sql server 2008.

I have a TabControl with 2 pages named

1)tblSupplier containing labels,textboxes,datetimepicker and combobox controls.

2)tblItems contains a checkedlistbox named LstProduct which contains datas from my another table in sql server 2008 named : item_master.

I populate my checkedlistbox in c# windows forms with sql server 2008 through following 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.SqlClient;
namespace WindowsFormsApplication1
{
    public partial class frmPatient : Form
    {
        int patientID;
        int pUserID;
        public frmPatient()
        {
            InitializeComponent();
            SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=SEIOS;Integrated Security=true");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            string SqlDataPull = ("Select agn,item_name from item_master where status=1");
            SqlCommand cmd = new SqlCommand(SqlDataPull);
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                SqlDataPull = dr[0].ToString() + dr[1].ToString();
                LstProduct.Items.Add(SqlDataPull);
            }
            dr.Close();
        }

where frmPatient is name of my form.

In my form named: frmPatient

i insert datas into tables named:address,supplier_id,supplier_name,supplier_contact and supplierproduct through following code in my button click event. My button name: btnCreate ,text: CREATE

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 WindowsFormsApplication1
{
    public partial class frmPatient : Form
    {
        int patientID;
        int pUserID;
        public frmPatient()
        {
            InitializeComponent();
            SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=SEIOS;Integrated Security=true");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            string SqlDataPull = ("Select agn,item_name from item_master where status=1");
            SqlCommand cmd = new SqlCommand(SqlDataPull);
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                SqlDataPull = dr[0].ToString() + dr[1].ToString();
                LstProduct.Items.Add(SqlDataPull);
            }
            dr.Close();
        }

 private void btnCreate_Click(object sender, EventArgs e)
        {
            string dFieldName = "";
            Boolean vEmptyB = false;
            if (txtFName.Text.ToString().Trim() == "")
            {
                vEmptyB = true;
                dFieldName = "Please enter Supplier name";
            }
            else if (cboType.SelectedIndex == -1)
            {
                vEmptyB = true;
                dFieldName = "Please select Supplier type";
            }
            else if (txtCountry.Text.ToString().Trim() == "")
            {
                vEmptyB = true;
                dFieldName = "Please enter the country";
            }
            if (vEmptyB == true)
            {
                MessageBox.Show(dFieldName + "should not be empty");
                return;
            }
            SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=SEIOS;Integrated Security=true");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            if (patientID == 0)
            {
                cmd = new SqlCommand("Insert into supplier_id(supplier_type,supplier_dOB,row_upd_date,user_id)" + "Values(@supplier_type,@supplier_dOB,GetDate(),@user_id)", conn);
                cmd.Parameters.AddWithValue("@supplier_type", cboType.SelectedIndex);
                cmd.Parameters.AddWithValue("@supplier_dOB", dtDOB.Value);
                cmd.Parameters.AddWithValue("@user_id", pUserID);
                cmd.ExecuteNonQuery();
            }
              int vPatientID;
              if (patientID == 0)
              {
                  vPatientID = Convert.ToInt32(cmd.Parameters.AddWithValue("@supplier_id", SqlDbType.Int).Value);
              }
              else
              {
                  cmd = new SqlCommand("Update supplier_name set status=0 where supplier_id=" + patientID, conn);
                  vPatientID = patientID;
              }
              cmd = new SqlCommand("Insert into supplier_name(supplier_id,supplier_name,supplier_contact_person,supplier_sale_name,status,row_upd_date,user_id)" + "Values(@supplier_id,@supplier_name,@supplier_contact_person,@supplier_sale_name,@status,GetDate(),@user_id)", conn);
              cmd.Parameters.AddWithValue("@supplier_id", vPatientID);
              cmd.Parameters.AddWithValue("@supplier_name", txtFName.Text.ToString());
              cmd.Parameters.AddWithValue("@supplier_contact_person", txtMName.Text.ToString());
              cmd.Parameters.AddWithValue("@supplier_sale_name", txtLName.Text.ToString());
              cmd.Parameters.AddWithValue("@status", 1);
              cmd.Parameters.AddWithValue("@user_id", pUserID);
              cmd.ExecuteNonQuery();
              if ((txtHNumber.Text != "") || (txtMNumber.Text != ""))
              {
                  if (patientID == 0)
                  {
                  cmd = new SqlCommand("Update supplier_contact set status=0 where supplier_id=" + patientID, conn);
                  }
                  cmd = new SqlCommand("Insert into supplier_contact(supplier_id,homenumber,mobilenumber,status,row_upd_date,user_id)" + "Values(@supplier_id,@homenumber,@mobilenumber,@status,GetDate(),@user_id)", conn);
                  cmd.Parameters.AddWithValue("@supplier_id", vPatientID);
                  cmd.Parameters.AddWithValue("@homenumber", txtHNumber.Text.ToString());
                  cmd.Parameters.AddWithValue("@mobilenumber", txtMNumber.Text.ToString());
                  cmd.Parameters.AddWithValue("@status", 1);
                  cmd.Parameters.AddWithValue("@user_id", pUserID);
                  cmd.ExecuteNonQuery();
              }
            if (patientID == 0)
            {
              cmd = new SqlCommand("Update address set status=0 where supplier_id=" + patientID, conn);
            }
            cmd = new SqlCommand("Insert into address(supplier_id,apartment_number,apartment_name,door_number,street_name_1,Street_name_2,Street_name_3,village,city,state,country,pincode,status,row_upd_date,user_id)" + "Values(@supplier_id,@apartment_number,@apartment_name,@door_number,@street_name_1,@Street_name_2,@Street_name_3,@village,@city,@state,@country,@pincode,@status,GetDate(),@user_id)", conn);
            cmd.Parameters.AddWithValue("@supplier_id", vPatientID);
            cmd.Parameters.AddWithValue("@apartment_number", txtApartmentNo.Text.ToString());
            cmd.Parameters.AddWithValue("@apartment_name", txtApartmentName.Text.ToString());
            cmd.Parameters.AddWithValue("@door_number", txtDoorNo.Text.ToString());
            cmd.Parameters.AddWithValue("@street_name_1", txtStreet1.Text.ToString());
            cmd.Parameters.AddWithValue("@Street_name_2", txtStreet2.Text.ToString());
            cmd.Parameters.AddWithValue("@Street_name_3", txtStreet3.Text.ToString());
            cmd.Parameters.AddWithValue("@village", txtVillageArea.Text.ToString());
            cmd.Parameters.AddWithValue("@city", txtCity.Text.ToString());
            cmd.Parameters.AddWithValue("@state", txtState.Text.ToString());
            cmd.Parameters.AddWithValue("@country", txtCountry.Text.ToString());
            cmd.Parameters.AddWithValue("@pincode", txtPCode.Text.ToString());
            cmd.Parameters.AddWithValue("@status", 1);
            cmd.Parameters.AddWithValue("@user_id", pUserID);
            cmd.ExecuteNonQuery();
            if (patientID == 0)
            {
                cmd = new SqlCommand("Update supplierproduct set status=0 where supplier_id=" + patientID, conn);
            }
            foreach (var chk in LstProduct.CheckedItems)
            {
                cmd = new SqlCommand("Insert into supplierproduct(supplier_id,product_id,status,row_upd_date,user_id)" + "Values(@supplier_id,@product_id,@status,GetDate(),@user_id)", conn);
                cmd.Parameters.AddWithValue("@supplier_id", vPatientID);
                cmd.Parameters.AddWithValue("@product_id", chk);
                cmd.Parameters.AddWithValue("@status", 1);
                cmd.Parameters.AddWithValue("@user_id", pUserID);
                cmd.ExecuteNonQuery();
            }
            ((MDIParent1)this.MdiParent).updateUserActivities(vPatientID, 1, txtFName.Text.ToString() + "supplier detail was added successfully");
            MessageBox.Show("Supplier Detail was added successfully", "SIOS", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            this.Close();

              
                
        }
    }

where SEIOS is name of my database in sql server 2008.

The area where i have problem in executing code in c# windows form with sql server 2008 is below:

 foreach (var chk in LstProduct.CheckedItems)
            {
                cmd = new SqlCommand("Insert into supplierproduct(supplier_id,product_id,status,row_upd_date,user_id)" + "Values(@supplier_id,@product_id,@status,GetDate(),@user_id)", conn);
                cmd.Parameters.AddWithValue("@supplier_id", vPatientID);
                cmd.Parameters.AddWithValue("@product_id", chk);
                cmd.Parameters.AddWithValue("@status", 1);
                cmd.Parameters.AddWithValue("@user_id", pUserID);
                cmd.ExecuteNonQuery();
            }

when i execute the code above i get error saying "SqlException was unhandled"

"Conversion failed when converting the nvarchar value '13Insulated Rods' to data type int."

where 13Insulated Rods is my item name value along with it's agn value from table name:item_master in sql server 2008.

Given below is my structure of tables item_master and supplierproduct in sql server 2008.

table name:item_master

Column Name                                               DataType

user_id                                                             Int(allow nulls)

row_upd_date                                                datetime(allow nulls)

deleted_status                                                bit(allow nulls)

status                                                              bit(allow nulls)

item_name                                                     nvarchar(250)(allow nulls)

item_parent                                                   Int(allow nulls)

item_price                                                      float(allow nulls)

item_desc1                                                   ntext(allow nulls)

item_desc2                                                  ntext(allow nulls)

item_desc3                                                  ntext(allow nulls)

item_type                                                      Int(allow nulls)

item_code                                                     nvarchar(10)(allow nulls)

agn                                                               Int(Primary key not null)

measure_type                                               nvarchar(25)(allow nulls)

table name:supplierproduct

Column Name                                                                 DataType

agn                                                                                   Int(Primary key not null)

supplier_id                                                                        Int(allow nulls)

product_id                                                                        Int(allow nulls)

row_upd_date                                                                 datetime(allow nulls)

status                                                                               bit(allow nulls)

user_id                                                                             Int(allow nulls)

I understand there is a problem of conversion between tables item_master and supplierproduct. But i dont know how to fix it. Can anybody help me please? Any help or guidance in solving this problem would be greatly appreciated.


vishal


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>