|
-
August 3rd, 2007, 09:39 AM
#1
Inserting data
The code below is for a button which needs to send data to a sql DB
I have two questions:
1 Does anyone have a sample code snippet that i can look at to see how to do this
2 would it be better to pass the variables to a stored procedure
and have the DB do the insert
or
should i just code it in as an insert into
My initial instinct is to use a stored procedure as that would make it easier
to upgrade/change in the future if needed.
Code:
private void Submit_btn_Click_1(object sender, EventArgs e)
{
//submit data to the desc_table
/*connecting to database to perform task
* connection variable. open();
* function
* .close(); */
myconnection.Open();
//do stuff
myconnection.Close();
}
Thanks all in advance
-
August 3rd, 2007, 12:15 PM
#2
Re: Inserting data
stored procedures are a bit better, that way if you got to change something you can usually get away without having to re-comple your app.
firstly you need to define what your using. You going to use Sql or ODBC?
heres a MSDN Sample : http://msdn2.microsoft.com/en-us/lib...88(VS.71).aspx
hth,
mcm
rate my posts!
mcm
-
August 3rd, 2007, 12:59 PM
#3
Re: Inserting data
 Originally Posted by mcmcom
stored procedures are a bit better, that way if you got to change something you can usually get away without having to re-comple your app.
firstly you need to define what your using. You going to use Sql or ODBC?
heres a MSDN Sample : http://msdn2.microsoft.com/en-us/lib...88(VS.71).aspx
hth,
mcm
I am using SQL server. Thanks for the link but that is for an asp.net page and I am building a windows app
-
August 3rd, 2007, 01:11 PM
#4
Re: Inserting data
well its pretty much the exact same code for a windows form, the calls to SQL server are irralavant to the fact that its win forms or a web app. you still need a :
Connection Object
Command Object
Parameters
Execution block
Code:
using(SqlConnection conn = new SqlConnection(ConfigruationManager.ConnectionStrings["myConStr"])){
SqlCommand cmd = new SqlCommand("INSERT STATEMENT", conn);
cmd.Parameters.AddWithValue("@myParm1",Value1);
....
try{
conn.open();
cmd.ExecuteNonQuery();
conn.close()
}
catch(SqlException ex){
throw(ex);
}
}
hth,
mcm
}
rate my posts!
mcm
-
August 3rd, 2007, 05:24 PM
#5
Re: Inserting data
Thanks
I will give it a try and let you know how it goes.
-
August 4th, 2007, 03:02 PM
#6
Re: Inserting data
Well I have gotten the code to compile.. thats good but when I Start debugging I get the following error
No mapping exists from object type System.Windows.Forms.TextBox to a known managed provider native type.
from the line
Code:
Submit_btn.ExecuteNonQuery();
Not really sure what is causing this. Here is the code for the button
Code:
private void Submit_btn_Click(object sender, EventArgs e)
{
myconnection.Open();
SqlCommand Submit_btn = new SqlCommand("mp_submit", myconnection);
Submit_btn.CommandType = CommandType.StoredProcedure;
//Submit_btn.Parameters.AddWithValue("@Description", Desc_cb);
Submit_btn.Parameters.AddWithValue("@Call", Call_box);
Submit_btn.ExecuteNonQuery();
myconnection.Close();
}
I even commented out a line and rewrote the stored procedure and created a new table just to see if I could insert anything.
-
August 4th, 2007, 05:00 PM
#7
Re: Inserting data
Thanks for all the help!!
I went back over the code and someone noticed that I had forgotten the in the line
Code:
Submit_btn.Parameters.AddWithValue("@Call",Call_box.Text)
-
August 4th, 2007, 06:12 PM
#8
Re: Inserting data
just a quick note. i would not name your Sql Command after your button. It may end up causing confusion when you look and notice you have multiple objects with the same name.
just name the command cmd or something like that
SqlCommand cmd = new SqlCommand <- lots of people just use this.
mcm
rate my posts!
mcm
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|