Click here to Skip to main content
15,896,278 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
So I'm trying to store data into a SQL database I made, but the code I'm using isn't working. I'm trying to get the data stored into tables but I have no idea what to use for it.

Heres the code I've done so far. I want to get the text boxes stored into individual tables. How do I do this?

private void btndatabase_Click(object sender, EventArgs e)
{
    SqlCommand cmd; 
    SqlConnection con = new SqlConnection("Data Source=MASTER/MASTER;initial catalog=MewsDB;uid=sa;");
    con.Open();
    cmd = new SqlCommand("insert Patient details '" + 
                         txtPatientName.Text + "',
                         '" + txtRespScore.Text + "',
                         '" + txtHeartScore.Text + "',
                         '" + txtBloodScore.Text + "',
                         '" + txtTempScore.Text + "',
                         '" + txtConsciousScore.Text +"',
                         '" + txtUrineScore.Text + "',
                         '" + txtMewsScore.Text + ")", con);
    cmd.ExecuteNonQuery();
    con.Close();
}
Posted
Updated 20-Jan-11 9:05am
v3
Comments
fjdiewornncalwe 20-Jan-11 15:11pm    
Based on what you post here and your previous question, I calculate that you spent less than 1 hour trying to figure this out for yourself. You obviously didn't do any research(ie google search, etc) and you obviously are just wanting someone else to write your code, so you get a -1 from me.

0) If any of those text boxes are empty, your code will fail.

1) You really should put a try/catch block around the whole thing.

2) Is it failing, or are you just here asking if the code looks alright?
 
Share this answer
 
Comments
programmer1234 20-Jan-11 14:53pm    
No the textboxes arent empty and it is failing.
fjdiewornncalwe 20-Jan-11 15:02pm    
Sorry John, but this is a very rare opportunity for me to tell you that you missed the blatantly obvious error in his sql itself, but yet you are correct on the next issue that I'm sure will arise in his code. See Griff's answer.
Try:
INSERT INTO "table_name" ("column1", "column2", ...) VALUES ("value1", "value2", ...)
as your SQL command. By preference, do it using Parameterised queries:
C#
cmd = new SqlCommand("INSERT INTO PatientDetails (Name, HeartScore) VALUES (@NAME, @HEARTSCORE)", con);
cmd.Parameters.AddWithValue("@NAME", txtPatientName.Text);
cmd.Parameters.AddWithValue("@HEARTSCORE", txtHeartScore.Text);
cmd.ExecuteNonQuery();
This assumes that "PatientDetails" is your Table, and two of the columns are "Name" and "HeartScore" - you can extend it to have all your fields.
It's a bit complicated to explain why at this stage, but it has to do with avoiding problems with your database caused by bad data entered in the textbox. Google for "SQL Injection Attack" for details when you are a bit more up on SQL. Plus it makes the lines shorter and more readable!
 
Share this answer
 
Comments
Espen Harlinn 20-Jan-11 14:58pm    
5+ Spot on, reading the code provided by OP obviously helps ...
Manfred Rudolf Bihy 20-Jan-11 15:59pm    
5+ Well spotted!

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