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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.SqlC onnection( connection String);
string queryString = "INSERT INTO [authors] ([au_id], [phone], [address]) VALUES (?, ?, ?)";
System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlC ommand();
dbCommand.CommandText = queryString;
dbCommand.Connection = dbConnection;
dbCommand.Parameters.Add(n ew SqlParameter("au_id", SqlDbType.NVarChar));;
dbCommand.Parameters["au_i d"].Value = au_id;
dbCommand.Parameters.Add(n ew SqlParameter("phone", SqlDbType.NVarChar));;
dbCommand.Parameters["phon e"].Value = phone;
dbCommand.Parameters.Add(n ew SqlParameter("address", SqlDbType.NVarChar));;
dbCommand.Parameters["addr ess"].Valu e = address;
int rowsAffected = 0;
dbConnection.Open();
try {
rowsAffected = dbCommand.ExecuteNonQuery( );
}
finally {
dbConnection.Close();
}
return rowsAffected;
}
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.SqlC
string queryString = "INSERT INTO [authors] ([au_id], [phone], [address]) VALUES (?, ?, ?)";
System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlC
dbCommand.CommandText = queryString;
dbCommand.Connection = dbConnection;
dbCommand.Parameters.Add(n
dbCommand.Parameters["au_i
dbCommand.Parameters.Add(n
dbCommand.Parameters["phon
dbCommand.Parameters.Add(n
dbCommand.Parameters["addr
int rowsAffected = 0;
dbConnection.Open();
try {
rowsAffected = dbCommand.ExecuteNonQuery(
}
finally {
dbConnection.Close();
}
return rowsAffected;
}
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
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
==========================
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.SqlC
string queryString = "UPDATE [authors] SET [phone]=@phone, [address]=@address WHERE ([authors].[au_id] " + "= @au_id)";
System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlC
dbCommand.CommandText = queryString;
dbCommand.Connection = dbConnection;
System.Data.IDataParameter
dbParam_au_id.ParameterNam
dbParam_au_id.Value = au_id;
dbParam_au_id.DbType = System.Data.DbType.String;
dbCommand.Parameters.Add(d
System.Data.IDataParameter
dbParam_phone.ParameterNam
dbParam_phone.Value = phone;
dbParam_phone.DbType = System.Data.DbType.StringF
dbCommand.Parameters.Add(d
System.Data.IDataParameter
dbParam_address.ParameterN
dbParam_address.Value = address;
dbParam_address.DbType = System.Data.DbType.String;
dbCommand.Parameters.Add(d
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-117
}
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.SqlC
string queryString = "UPDATE [authors] SET [phone]=@phone, [address]=@address WHERE ([authors].[au_id] " + "= @au_id)";
System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlC
dbCommand.CommandText = queryString;
dbCommand.Connection = dbConnection;
System.Data.IDataParameter
dbParam_au_id.ParameterNam
dbParam_au_id.Value = au_id;
dbParam_au_id.DbType = System.Data.DbType.String;
dbCommand.Parameters.Add(d
System.Data.IDataParameter
dbParam_phone.ParameterNam
dbParam_phone.Value = phone;
dbParam_phone.DbType = System.Data.DbType.StringF
dbCommand.Parameters.Add(d
System.Data.IDataParameter
dbParam_address.ParameterN
dbParam_address.Value = address;
dbParam_address.DbType = System.Data.DbType.String;
dbCommand.Parameters.Add(d
int rowsAffected = 0;
dbConnection.Open();
try {
rowsAffected = dbCommand.ExecuteNonQuery(
}
finally {
dbConnection.Close();
}
return rowsAffected;
}
}
}
==========================
itsvtk