Link to home
Start Free TrialLog in
Avatar of toms4u
toms4u

asked on

VC#.NET - insertion and updation of information through textboxes to the SQL SERVER 2000


hello sir,
i am new to the visual C#.net.
I have one form  and two text boxes .Textbox1 is used to see the address of a student  and textbox2  is used for show the contact number of the student..I want to store these two information  on the SQL server..through the form at runtime.I also have a button in the form to update these two  fields. I need the code for this problem in VC#.NET .Coluld  anybody please help me ......

       thanking you

tom sebastian
newdelhi
India
ASKER CERTIFIED SOLUTION
Avatar of Thandava Vallepalli
Thandava Vallepalli
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is ur update function for updating authors table by sending au_id, phone, address in PUBS database in SQL Server 2000...

====================================================
        public static int MyUpdateMethod(string au_id, string phone, string address)
        {
            string connectionString = "server=\'(local)\'; trusted_connection=true; database=\'pubs\'";
            System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(connectionString);

            string queryString = "UPDATE [authors] SET [phone]=@phone, [address]=@address WHERE ([authors].[au_id] " + "= @au_id)";
            System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
            dbCommand.CommandText = queryString;
            dbCommand.Connection = dbConnection;

            System.Data.IDataParameter dbParam_au_id = new System.Data.SqlClient.SqlParameter();
            dbParam_au_id.ParameterName = "@au_id";
            dbParam_au_id.Value = au_id;
            dbParam_au_id.DbType = System.Data.DbType.String;
            dbCommand.Parameters.Add(dbParam_au_id);
            System.Data.IDataParameter dbParam_phone = new System.Data.SqlClient.SqlParameter();
            dbParam_phone.ParameterName = "@phone";
            dbParam_phone.Value = phone;
            dbParam_phone.DbType = System.Data.DbType.StringFixedLength;
            dbCommand.Parameters.Add(dbParam_phone);
            System.Data.IDataParameter dbParam_address = new System.Data.SqlClient.SqlParameter();
            dbParam_address.ParameterName = "@address";
            dbParam_address.Value = address;
            dbParam_address.DbType = System.Data.DbType.String;
            dbCommand.Parameters.Add(dbParam_address);

            int rowsAffected = 0;
            dbConnection.Open();
            try {
                rowsAffected = dbCommand.ExecuteNonQuery();
            }
            finally {
                dbConnection.Close();
            }

            return rowsAffected;
        }
==============================

replace db connection information, table information as per ur requirements

itsvtk


if you have pubs db access execute this program..
=======================================


namespace ns {
    using System;

    public class demo {

        public static void Main()
        {
            Console.WriteLine( MyUpdateMethod("172-32-1176", "408 496-7223", "10932 Bigge Rd.") );
        }

        public static int MyUpdateMethod(string au_id, string phone, string address)
        {
            string connectionString = "server=\'(local)\'; trusted_connection=true; database=\'pubs\'";
            System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(connectionString);

            string queryString = "UPDATE [authors] SET [phone]=@phone, [address]=@address WHERE ([authors].[au_id] " + "= @au_id)";
            System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
            dbCommand.CommandText = queryString;
            dbCommand.Connection = dbConnection;

            System.Data.IDataParameter dbParam_au_id = new System.Data.SqlClient.SqlParameter();
            dbParam_au_id.ParameterName = "@au_id";
            dbParam_au_id.Value = au_id;
            dbParam_au_id.DbType = System.Data.DbType.String;
            dbCommand.Parameters.Add(dbParam_au_id);
            System.Data.IDataParameter dbParam_phone = new System.Data.SqlClient.SqlParameter();
            dbParam_phone.ParameterName = "@phone";
            dbParam_phone.Value = phone;
            dbParam_phone.DbType = System.Data.DbType.StringFixedLength;
            dbCommand.Parameters.Add(dbParam_phone);
            System.Data.IDataParameter dbParam_address = new System.Data.SqlClient.SqlParameter();
            dbParam_address.ParameterName = "@address";
            dbParam_address.Value = address;
            dbParam_address.DbType = System.Data.DbType.String;
            dbCommand.Parameters.Add(dbParam_address);

            int rowsAffected = 0;
            dbConnection.Open();
            try {
                rowsAffected = dbCommand.ExecuteNonQuery();
            }
            finally {
                dbConnection.Close();
            }

            return rowsAffected;
        }

    }
}

============================

itsvtk


Based on itsvtk solution, here is the insert function

public static int MyInsertMethod(string au_id, string phone, string address)
        {
            string connectionString = "server=\'(local)\'; trusted_connection=true; database=\'pubs\'";
            System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(connectionString);

            string queryString = "INSERT INTO [authors] ([au_id], [phone], [address]) VALUES (?, ?, ?)";
            System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
            dbCommand.CommandText = queryString;
            dbCommand.Connection = dbConnection;

dbCommand.Parameters.Add(new SqlParameter("au_id", SqlDbType.NVarChar));;
dbCommand.Parameters["au_id"].Value = au_id;

dbCommand.Parameters.Add(new SqlParameter("phone", SqlDbType.NVarChar));;
dbCommand.Parameters["phone"].Value = phone;

dbCommand.Parameters.Add(new SqlParameter("address", SqlDbType.NVarChar));;
dbCommand.Parameters["address"].Value = address;
            int rowsAffected = 0;
            dbConnection.Open();
            try {
                rowsAffected = dbCommand.ExecuteNonQuery();
            }
            finally {
                dbConnection.Close();
            }

            return rowsAffected;
        }
Avatar of toms4u
toms4u

ASKER

hello sir,
i am getting some errors.("system error")
i  am explaining  my practice project  conditions......

SQL Server Service Manager  Interface showing like this....
Server   : TOMS\TOMS
Services:  SQL Server
SQL is installed on a desktop XP system


Form1 Details:
label1(text=enter your name)                    corresponding textbox1
label2(text=enter your address)                corresponding textbox2
label3(text=enter your phone number)      correspondig textbox3

button1(text=update)
button2(text=show details)
button3(text=insert)
button4(text=next)

database details:
servername: TOMS\TOMS
user id: sa
password :123456

database name :  toms
table name  :users
columns are  :STUDENT_ID,ADDRESS,PHONE

i already inserted  2 rows of information in this table.


my needs are:
          ( 1)    i am entering a name in the  'textbox1'  and pressing ' showdetails button '.then i want to show   the address  and phone informations on the corresponding textboxes....then if i am pressing
' next button' then i want to  show  next row.....
   
          (2) when i am entering the all 3 fields  and pressing the insert button   then i want to insert data into the  same table 'users' in the same database ' toms' and show a message "your data saved".
           (3) now i want to change address  and phone details  for a particular user ...for this i will enter a name ,address and phone in the corresponding textboxes.....if this name is not in the  users table , i want to throw an exception  message "the name does not exists"  continue the processes....otherwise do update the database  and show a message "your data has been updated..."



 hopely waiting for a solution in VC#.NET in connection oriented  data access method and disconnected data access method....in exact coding with my  details  because i  am very beginner
please give me a replay as soon as possible

toms