using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient; 

namespace DataClerk
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtPNo.Clear();
            txtPName.Clear();
            txtSiteName.Clear();
            txtJCNo.Clear();
            txtRequired.Clear();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }
        public void Save()
        {
            //create connection string
            string connstring = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=fairdealdbAnalysis;Data Source=RECEPTION\\SQLEXPRESS";

            //create connection to teh sql server database
            SqlConnection sqlconn =new SqlConnection(connstring);

            
            try
            {
                //open the connection
                if(sqlconn.State!=ConnectionState.Open)
                {
                    sqlconn.Open();
                }
                SqlCommand sqlcom = sqlconn.CreateCommand();
                StringBuilder builder = new StringBuilder();

                builder.Append("'"+txtPNo.Text+"',");
                builder.Append("'"+txtPName.Text+"',");
                builder.Append("'"+txtSiteName.Text+"',");
                builder.Append("'"+txtJCNo.Text+"',");
                builder.Append("'"+txtRequired.Text+"'");

                sqlcom.CommandText ="INSERT INTO tblFormSave(dateIssued,projectNumber,projectName,jobCardNumber,siteName,RequiredBy) VALUES("+builder.ToString()+") ";

                if(Convert.ToBoolean(sqlcom.ExecuteNonQuery()))
                {
                    MessageBox.Show("Record Saved");
                }
                else
                {
                    MessageBox.Show("Record not Saved");
                }
            

            }
            catch{}
            finally
            {
                if(sqlconn.State!=ConnectionState.Closed)
                    sqlconn.Close();
            }
        }
    }
}

i was creating a code that wen one says save the data entered is saved into the sql server database.Kindly have a look at this.
i am getting errors...especially with the sql connection.

Recommended Answers

All 13 Replies

here there is only 5 tetxtbox and u passing 6 values in sql query
and m try into modifing ur code try my code must be work and u have to do litel bit changes as per ur datatype in database like if datatype is varchar write it in '' ok

try
{
//open the connection
if(sqlconn.State!=ConnectionState.Open)
{
sqlconn.Open();
}
SqlCommand sqlcom = sqlconn.CreateCommand();


sqlcom.CommandText ="INSERT INTO tblFormSave(dateIssued,projectNumber,projectName,jobCardNumber,siteName,RequiredBy) 
VALUES(insert issue date parameter,"+txtPNo.Text+","+txtPName.Text+","+txtSiteName.Text+","+txtJCNo.Text+","+txtRequired.Text+") ";

if(Convert.ToBoolean(sqlcom.ExecuteNonQuery()))
{
MessageBox.Show("Record Saved");
}
else
{
MessageBox.Show("Record not Saved");
}


}

hey friend remove

Provider=SQLOLEDB.1;

''this thing

string connstring = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=fairdealdbAnalysis;Data Source=RECEPTION\\SQLEXPRESS";

from your connection string . i think error is there no need to write provider

yes, even hiren is right there are six column in your table and you are inserting five value so how can you save it will give you error.

actually i removed the extra column and still got the same error...i don't understand the comments about the connection string.actually am certain the problem is originating from the connection string because i gr this error

Argument ExceptionUnhandled
Keyword not supported: 'provider'....while pointing at

SqlConnection sqlconn =new SqlConnection(connstring);

..

if i understand the problem am sure i'll be able to sort the problem


Regards

actually i removed the extra column and still got the same error...i don't understand the comments about the connection string.actually am certain the problem is originating from the connection string because i gr this error

Argument ExceptionUnhandled
Keyword not supported: 'provider'....while pointing at

SqlConnection sqlconn =new SqlConnection(connstring);

..

if i understand the problem am sure i'll be able to sort the problem


Regards

Do not use "Provider" type connection string with System.Data.SqlClient classes. ConnectionString used in your code-snippet must be used System.Data.OleDb data provider classes.

Take a look at MSSQL Server connection string - http://www.connectionstrings.com/sql-server-2005

i don't understand you adatapost...do you mean i change me provider to oledb.

Change connectionString value. It should be,

string connstring = "Data Source=RECEPTION\\SQLEXPRESS;Initial Catalog=fairdealdbAnalysis;Integrated Security=SSPI;Persist Security Info=False;";

thank you very much its not bringing any error but the data is not saved on the db and the it does not say record saved nor record not saved as i coded.the application continues to run for a long time

can you show me the code then i can help you.other wise use System.Transaction class
for DML query.
When you start connection say begin transaction
first create a object
SqlTransaction trans =new SqlTransaction ();
trans.BeginTransaction(//Connection string);
try
{//pass trans with your command
trans.Commit();
}
catch
{
trans.rollback();
}
like this

this is the code.i noticed that this project will be in a different computer from the computer hosting the sql server.thats why i have put the ip address of the server.but kindly have a look at the code

i appreciate totaly

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient; 

namespace DataClerk
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtPNo.Clear();
            txtPName.Clear();
            txtSiteName.Clear();
            txtJCNo.Clear();
            txtRequired.Clear();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }
        public void Save()
        {
            //create connection string
            //string cs = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=fairdealdbAnalysis;Data Source=RECEPTION\\SQLEXPRESS";//"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=fairdealdbAnalysis;Data Source=RECEPTION\\SQLEXPRESS";

            string connstring ="Data Source=192.168.1.13,1025;Network Library=DBMSSOCN;Initial Catalog=myDataBase;Integrated Security=SSPI;Persist Security Info=False;";
            //string connstring = "Data Source=RECEPTION\\SQLEXPRESS;Initial Catalog=fairdealdbAnalysis;Integrated Security=SSPI;Persist Security Info=False;";

            //create connection to teh sql server database
            SqlConnection sqlconn =new SqlConnection(connstring);

            
            try
            {
                //open the connection
                if(sqlconn.State!=ConnectionState.Open)
                {
                    sqlconn.Open();
                }
                SqlCommand sqlcom = sqlconn.CreateCommand();
                //StringBuilder builder = new StringBuilder();

                //builder.Append("'"+txtPNo.Text+"',");
                //builder.Append("'"+txtPName.Text+"',");
                //builder.Append("'"+txtSiteName.Text+"',");
                //builder.Append("'"+txtJCNo.Text+"',");
                //builder.Append("'"+txtRequired.Text+"'");

                sqlcom.CommandText ="INSERT INTO tblFormSave(projectNumber,projectName,jobCardNumber,siteName,RequiredBy) VALUES("+txtPNo.Text+","+txtPName.Text+","+txtJCNo.Text+","+txtSiteName.Text+","+txtRequired.Text+",)"; //"+builder.ToString()+") ";

                if(Convert.ToBoolean(sqlcom.ExecuteNonQuery()))
                {
                    MessageBox.Show("Record Saved");
                }
                else
                {
                    MessageBox.Show("Record not Saved");
                }
            

            }
            catch{}
            finally
            {
                if(sqlconn.State!=ConnectionState.Closed)
                    sqlconn.Close();
            }
        }
    }
}

hey...it works...it finally works...i returned to using string builder and it has finally worked...now its saving the data to the sql database......thank you all for you help.i wouldn't have made it without all of you.

i'mma try using the ipaddress...hope twill still work.it has worked while at the server computer.if it works with data source form the ip address....i'll be very greatful.

thanks again.

Please mark this thread as solved if you have found an answer to your question and good luck!

hi everyone i was finally able to connect the forms' data to the sql server database from remote computer.i only needed to change connection string.thanks for all the help.

Kind Regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.