Monday, April 26, 2010

Insert data using sqlcommand




Insert data using sqlcommand,usingsimple,datasqlclassusing,sqlcommand,simpledatausing,datasqlcommand,usingsqlcommand,sqlcommandsqlclass,simplesqlclasssimple,sqlcommandusing,datasqlcommandsimple,sqlclassdata,simplesqlclasssqlcommand,simplesqlcommand,datasimple,sqlcommanddatasimple,usingdatasqlcommand,simplesqlclassusing,sqlcommanddatausing,usingsqlcommandsqlclass,

It is step3 procedure

Insert data using sqlcommand

Save button event
protected void btninsert_Click(object sender, EventArgs e)
{
first f = new first();
f.id = Convert.ToInt32(TextBox1.Text);
f.name = TextBox2.Text;
f.course = TextBox3.Text;
f.fee = Convert.ToInt32(TextBox4.Text);

datac c=new datac();
c.insertmain(f);
Label1.Text = "record inserted";
}



It is step4 procedure


Variable class

public class first
{
public int id{get;set;}

public string name { get; set; }
public string course { get; set; }
public int fee { get; set; }
}

It is step5 procedure

Data class

Public void insertmain(first f)
{
string s= "insert into student values(" + f.id + ",'" + f.name + "','" + f.course + "'," + f.fee + ")";

string constring = "Data Source= SHYAM-6E8C5C74B\\SQLEXPRESS ;initial catalog=college; integrated security=True ";

SqlConnection cn = new SqlConnection(constring);
SqlCommand cmd = new SqlCommand(cmd, cn);
cn,Open();
cmd.ExecuteNonQuery();
cn.Close();
}


















2 comments:

Ahmad Elayyan said...

thank you shayma, but a little bit, try to use stored procedure instead of ad-hoc statement to generate a sql statement.

like the following :

Create Procedure dbo.StudentInsert
(
@id int,
@name nvarchar(30),
@course nvarchar(30), -- change it to decimal
@fee varchar(8000)-- change it into decimal.
)
AS
Begin Try
insert into student (id, name, course, fee)
values(@id , @name, @course, @fee)

End Try
Begin Catch

SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_STATE() as ErrorState,
ERROR_PROCEDURE() as ErrorProcedure,
ERROR_LINE() as ErrorLine,
ERROR_MESSAGE() as ErrorMessage;

End Catch
go

shyam said...

I will upload data for stored procedure and linq also

due to lake of time i cant do that

keep watching i will upload soon....

Regards,
Shyam Parmar