-4

Why can't I store value in database? My code is correct. When I access the database, then there is no value shown. But when I execute, then it's implemented correct. Tell me what to do to make it correct.

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 System.Text.RegularExpressions;
using System.Windows.Input;
using System.Configuration;

namespace ODesk
{
    public partial class Form8registration : Form
    {

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);

        public Form8registration()
        {
            InitializeComponent();
        }

        private void linkLabel1_login_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Form1 fm1 = new Form1();
            fm1.Show();
            this.Hide();

        }

        private void Form8registration_Load(object sender, EventArgs e)
        {
            conn.Open();                

        }

        private void button2_reset_Click(object sender, EventArgs e)
        {
            Reset();
        }

        private void Reset()
        {
            textBox1_add.Text = "";
            textBox3_phoneno.Text = "";
            textBox4_emailid.Text = "";
            textBox5_desgination.Text = "";
            textBox6_name.Text = "";
            textBox7_empid.Text = "";
            comboBox1_shifttime.SelectedText  = "";
        }

        private void button3_cancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_submit_Click(object sender, EventArgs e)
        {
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }              

                    SqlCommand cmdd = new SqlCommand("registrtn", conn);

                    cmdd.CommandType = CommandType.StoredProcedure;
                    cmdd.Connection = conn;

                    cmdd.Parameters.Add("@empid",SqlDbType.VarChar,50).Value = textBox7_empid.Text;
                    cmdd.Parameters.Add("@name", SqlDbType.VarChar, 50).Value = textBox6_name.Text;
                    cmdd.Parameters.Add("@desgination", SqlDbType.VarChar, 50).Value = textBox5_desgination.Text;
                    cmdd.Parameters.Add("@emailid", SqlDbType.VarChar, 50).Value = textBox4_emailid.Text;
                    cmdd.Parameters.Add("@phone", SqlDbType.VarChar, 50).Value = textBox3_phoneno.Text;
                    cmdd.Parameters.Add("@skype", SqlDbType.VarChar, 50).Value = textBox1_skpid.Text;
                    cmdd.Parameters.Add("@address", SqlDbType.VarChar, 50).Value = textBox1_add.Text;
                    cmdd.Parameters.Add("@shifttime", SqlDbType.VarChar, 50).Value = comboBox1_shifttime.SelectedText;
                    cmdd.Parameters.Add("@panel", SqlDbType.VarChar, 50).Value = radioButton1_employe.Checked.ToString();


                    cmdd.ExecuteNonQuery();
                    cmdd.Dispose();
                    conn.Close();
                    Reset();
        }
    }
} 
nbanic
  • 1,270
  • 1
  • 8
  • 11
  • Have you verified that you are attaching as the user you expect, that the stored procedure exists, and that the user has access to the db and the procedure? – Noel Sep 25 '12 at 18:56
  • 5
    Clearly, your code is *not* correct. Please shorten the sample code to only what is necessary. – usr Sep 25 '12 at 18:58
  • what does the close method do? – Rafay Zia Mir Sep 25 '12 at 19:00
  • 1
    Your C# code looks ok (you don't need to set the cmdd.Connection = conn as it is already set) but what is in the stored procedure "registrtn" ? Does it encounter any errors in the button1_submit_Click method that would indicate there is a problem with the connection string or that the stored procedure doesn't exist/isn't available to the user? – d89761 Sep 25 '12 at 19:00
  • Please remove the unnecessary code and just show the most relevant one. Also show the details of SP registrtn – Furqan Safdar Sep 25 '12 at 19:01

1 Answers1

1

For all we know, you haven't wired up the button1_submit_Click event correctly!

I suggest you execute the code in button1_submit_Click with some simple test data and see if there is a problem with your database / permissions....(missing stored procedure, incorrect parameters) .. or whether there is an issue with your UI

Just run it without any interaction from the UI and that should help troubleshoot any issues.

Also try running the stored procedure in Management Studio and see what happens.

Have you considered using instead of what you are doing.

using(var conn = new SqlConnection(connString))
{
   SqlCommand cmdd = new SqlCommand("registrtn", conn);
   cmdd.CommandType = CommandType.StoredProcedure;
   cmdd.Parameters.Add("@empid",SqlDbType.VarChar,50).Value = textBox7_empid.Text;
   cmdd.Parameters.Add("@name", SqlDbType.VarChar, 50).Value = textBox6_name.Text;
   cmdd.Parameters.Add("@desgination", SqlDbType.VarChar, 50).Value = textBox5_desgination.Text;
   cmdd.Parameters.Add("@emailid", SqlDbType.VarChar, 50).Value = textBox4_emailid.Text;
   cmdd.Parameters.Add("@phone", SqlDbType.VarChar, 50).Value = textBox3_phoneno.Text;
   cmdd.Parameters.Add("@skype", SqlDbType.VarChar, 50).Value = textBox1_skpid.Text;
   cmdd.Parameters.Add("@address", SqlDbType.VarChar, 50).Value = textBox1_add.Text;
   cmdd.Parameters.Add("@shifttime", SqlDbType.VarChar, 50).Value = comboBox1_shifttime.SelectedText;
   cmdd.Parameters.Add("@panel", SqlDbType.VarChar, 50).Value = radioButton1_employe.Checked.ToString();

   cmdd.ExecuteNonQuery();
}
Community
  • 1
  • 1
Ian G
  • 29,468
  • 21
  • 78
  • 92
  • its not working the value can not be store in the database – Kay Pee Singh Sep 25 '12 at 19:58
  • yes i tried thats why am saying to u ,,,, and in store procedure when iu executing then its implemented correct and also it stored in the database ,,,,but when i run my window form after puting the value in fields then its not shown in the database – Kay Pee Singh Sep 26 '12 at 14:41