into database.
Is there any other way except using parameters? and if there isn't, how can i insert 2 parameters in the same query?
Please be more specific: Are you trying to insert a record into a table doing raw SQL? (using a "SqlConnection" and a "SqlCommand" field?)
Or are you using a SqlDataSource and trying to tap into the "InsertCommand" property?
Thanks,
|||while not recommended, you can dynamically create your sql string (insert statment) using your code...
if you want to do more than one parameter, just add a new one
ParameterCollection coll =new ParameterCollection();// add a parm coll.Add(new Parameter("blah", TypeCode.String,"hello")); coll.Add(new Parameter("other", TypeCode.Int16,"1"));string sql ="insert into something (field1, field1) values (@.blah, @.other)";You didn't mention if this was for a sqldatasource, objectdatasource or something else, so this is rather generic... but you can just add(new parameter()) to your sources parameters collection.|||
Sorry for not being specific, didn't know it makes a difference. here is an example code
using System;using System.Data;using System.Data.SqlClient;using System.Net;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partialclass Register : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e) { }protected void Button1_Click(object sender, EventArgs e) { SqlConnection conn =new SqlConnection("server=(local); database=phynew;integrated security=true"); conn.Open();try {string aaa ="test1";string bbb ="hello2"; SqlCommand myCommand =new SqlCommand(); myCommand.Connection = conn; myCommand.CommandType = CommandType.Text; myCommand.CommandText ="INSERT INTO tblAdmin (username, pwd) Values(aaa ,bbb)"; myCommand.ExecuteNonQuery(); }catch (SqlException err) { TextBox2.Text = err.ToString(); }finally { conn.Close(); } }}|||
Cool, you're almost done!
myCommand.CommandText ="INSERT INTO tblAdmin (username, pwd) Values(aaa ,bbb)";
That's your code... change it to this:
myCommand.CommandText ="INSERT INTO tblAdmin (username, pwd) Values(@.aaa ,@.bbb)";
Then all you have to do is this:
myCommand.Parameters.AddWithValue("@.aaa", "Hello!!!");
myCommand.Parameters.AddWithValue("@.bbb", 123);
You can put strings, numbers, a boolean or whatever as the value.
Peace, (and please mark this as the answer when you get it working)
|||thanks, it works now.
No comments:
Post a Comment