Click here to Skip to main content
15,914,594 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
How to insert data from textboxes to sql table in c#?
i tried this but i know it is not completely right

SqlCommand add_d = new SqlCommand("Insert into Dealer" + "values('" + D_B1.Text + "'," + D_B2.Text + "'," + D_B3.Text + "'," + D_B4.Text + "'," + D_B5.Text + "'," + D_B6.Text + ")", connection_a);
add_d.ExecuteNonQuery();


Dealer table also has a foreign key, how to deal with it?
Posted

Don't do this way use parametrized query or use SPs to avoid sql-injection attacks

you can do this way:


MIDL
string db1=textbox1.text;
string str = "Data Source=ABC-Pc\\SQLEXPRESS;Initial Catalog=mydb;Integrated Security=True";
 SqlConnection conn = new SqlConnection(str);
conn.open();

string insertquery = "insert into marksheets(dbfield1) values(@dbfield1)
SqlCommand cmd = new SqlCommand(insertquery, conn);
cmd.Parameters.AddWithValue("@dbfield1", db1);

cmd.ExecuteNonQuery();

like this you can add as many fields as you want.

hope this helps :)
for further queries comment here!!
 
Share this answer
 
Comments
Sweety Khan 15-Jun-11 9:47am    
ok by this its done thanx. i left that way but its a very short code. i wanna know how can i do this by SPs plz tell me
Sweety Khan 15-Jun-11 9:48am    
one more thing left tht how to deal with foriegn column
Uday P.Singh 15-Jun-11 10:21am    
which is your primary key table and field name for this foreign key table?
Sweety Khan 15-Jun-11 12:20pm    
Administrator is the primary key table and Admin_ID is its primary key. i make it (Admin_ID) foreign key in Dealer table.
Uday P.Singh 15-Jun-11 12:32pm    
u have to insert admin_id values of Administrator table in dealer table admin_id field, but make sure u have make it cascade at insert or update.
What error do you get?

First visible mistake is "Insert into Dealer" + "values('"
There would be now space between Dealer and values, which will definitely give an error. Change it to "Insert into Dealer" + " values('"

Secondly, though we don't know the data structure of table, if you have any numeric column in table, remove the enclosing ' marks from your command.

I hope this will help you. Try to learn more. You can use google.
 
Share this answer
 
Best way to do this is to use SPs. Also when you are inserting it is better to specify the columns you want to use.
 
Share this answer
 
Comments
Sweety Khan 15-Jun-11 5:21am    
oki. if im using all the columns then can i write like this? n sorry i dont understand wht do u mean by SPs
Uday P.Singh 15-Jun-11 5:31am    
with SPs he mean Stored Procedures
you can use this link to use SPs:

Insert and retrieve data through stored procedure[^]

hope this helps :)
 
Share this answer
 
Comments
Sweety Khan 15-Jun-11 12:51pm    
hmm quite similar to what u told as parametrized query

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900