Asp.net Insert problem

Posted by Self-innovator under ASP.NET on 12/25/2011 | Points: 10 | Views : 1028 | Status : [Member] | Replies : 1
hi,pls
go through this code (when i use stored proc it works fine but when i use cmd.commandtype=commandtyp.text not inserting the values)
public _insert(string pName,int pAge,string pCity)//Works fine
{
SqlConnection cnn = new SqlConnection(Conn);
SqlCommand cmd = new SqlCommand("Ins_P", cnn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@pName", pName);
cmd.Parameters.AddWithValue("@pAge", pAge);
cmd.Parameters.AddWithValue("@pCity", pCity);
SqlDataAdapter ada = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ada.Fill(ds);
return ds;
}

public _insert(string pName,int pAge,string pCity)//doesn't Works
{
SqlConnection cnn = new SqlConnection(Conn);
SqlCommand cmd = new SqlCommand("Insert into P values(pName,pAge,pCity)", cnn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@pName", pName);
cmd.Parameters.AddWithValue("@pAge", pAge);
cmd.Parameters.AddWithValue("@pCity", pCity);
SqlDataAdapter ada = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ada.Fill(ds);
return ds;
}
what should i used in 3rd statement instead of cmd.commandtype=commandtype.text pls tell me and the diff between these above two 3rd statements..

Join Hands Change lives
Thanks & Regards
Straight Edge Society



Responses

Posted by: Pradeep417 on: 12/26/2011 [Member] Starter | Points: 25

Up
0
Down

Hi,

There is no wrong in 3rd statement and you have write the query like this.

SqlCommand cmd = new SqlCommand("Insert into P(column name,column name,column name) values(pName,pAge,pCity)", cnn);

Difference:

CommandType.Text: Should be used when you are passing in a plain text TSQL statement,such as select,insert,update commands.
CommandType.StoredProcedure: Should be used when you are passing in the name of stored procedure to execute.

I hope this helps you.



Pradeep

Self-innovator, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response