?
Solved

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

Posted on 2005-03-15
4
Medium Priority
?
201 Views
Last Modified: 2010-04-16

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
0
Comment
Question by:toms4u
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
Thandava Vallepalli earned 1000 total points
ID: 13544190
// On button Click Event Write this code

SqlConnection con = new SqlConnection ( "SERVER=<IPADDRESS>;USER ID=<USER ID>;PASSWORD=<PW>;DATA BASE=<DATABASENAME>" );
SqlCommand com = new SqlCommand( "UPDATE <TABLENAME> SET FIELD1 = '" +  TextBox1.Value + "',  SET FIELD2 ='" + TextBox2.Text + "' WHERE STUDENT_ID = " + sid );
con.Open();
int res = com.ExecuteNonQuery();

if (res != 0)
   MessageBox.Show( "Number of Rows Updated=" + res );

con.Close();


itsvtk


0
 
LVL 14

Expert Comment

by:Thandava Vallepalli
ID: 13544281
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


0
 
LVL 13

Expert Comment

by:dungla
ID: 13546287
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;
        }
0
 

Author Comment

by:toms4u
ID: 13548201
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
   


 



0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Integration Management Part 2
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month12 days, 23 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question