Hello, im trying to insert data to sql server and its not working so well even the code not show any error message or errors on compile. when i check the database......hes empty. Please look at the code for sulotion:

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 WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True;");
            try
            {
                SqlCommand cmd = new SqlCommand("INSERT INTO customers(firstName,lastName,address,city) VALUES ('wee','weeee','dfddd', 'weee')", conn);
                conn.Open();
                cmd.ExecuteNonQuery();
                MessageBox.Show("Connection Succeful");
            }
            catch (SqlException ex)
            {

                MessageBox.Show("There is an Error" + ex);
            }
            finally
            {
                conn.Close();
                MessageBox.Show("Connection Closed");
            }
        }
    }
}

insert a break point to button_click event handler and then run the application in the debug mode, make sure to see if all the lines are executed. or attach your solution as zip file so we can test to see what's going on.

Look at the output window after you click the button. Do you see an exception similar to:

A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Hi

This Code is very useful to display data from textbox to sqldatabase

OnButton Click

Dim sqlStmt As String
Dim conString As String
Dim cn As Sqlconnection (Won't reconize SQL connection)
Dim cmd As SqlCommand (Won't reconize SQL command)
Try
sqlStmt = "insert into Table1 (FName) Values (@Fname) "
conString = "Server and connection here is correct;"
cn = New SqlConnection(conString)
cmd = New SqlCommand(sqlStmt, cn)
cmd.Parameters.Add(New SqlParameter("@FName", SqlDbType.NVarChar, 11))
cmd.Parameters("@FName").Value = TxtBoxNewRN.Text
cn.Open()
cmd.ExecuteNonQuery()
Finally
cn.Close()
End Try

ecommerce development company
Sharepoint Development
ASP.Net Development
PHP Development

I am trying to insert data of form 1 using form 2 into sql db but i am unable to do so using below given code.
in my code form 1 name (CrtAccount) and form 2 name (Nominee).

if (MessageBox.Show("Have you done with your job", "Done", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                                                                            {

                                                                            SqlConnection sqlsend = new SqlConnection(@"Data   Source=TRIMBAKKAR\SQLEXPRESS;Initial Catalog=OLB_DB;Integrated Security=True");
                                                                            sqlsend.Open();
                                                                            MessageBox.Show("connection open");
                                                                            string gen = "Female";
                                                                            if (NomMaleRdb.Checked)
                                                                            {
                                                                                gen = "Male";
                                                                            }

                                                                            string qr = "INSERT INTO olb_customerinfo (cus_fname, cus_mname, cus_lname, cus_moname, cus_gender, cus_dob, cus_marstatus, cus_education, cus_occupation, cus_nationality) VALUES ('" + CrtFnameTxt.Text + "','" + CrtMnameTxt.Text + "','" + CrtLnameTxt.Text + "','" + CrtMotnameTxt.Text + "','" + @gen + "'," + CrtDobTxt.Text + ",'" + CrtMarStatusCmb.SelectedItem + "','" + CrtEduTxt.Text + "','" + CrtOccuTxt.Text + "','" + CrtNationTxt.Text + "')";
                                                                            SqlCommand cmd = new SqlCommand(qr, sqlsend);

                                                                            cmd.ExecuteNonQuery();
                                                                            MessageBox.Show("inserted");
                                                                            sqlsend.Close();
                                                                            MessageBox.Show("Close");
                                                                            MDI form2 = new MDI();
                                                                            form2.MDILogutBt.Enabled = true;
                                                                            this.Hide();
                                                                        }
This article has been dead for over six months. Start a new discussion instead.