Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi anyone can answer to me ? I want send one parameter to my stored procedure that is in my database.PLEASE HELP ME
i wrote following codes:

C#
    string aname = TextBox1.Text; 
    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=NezamPezeshki;Integrated Security=True");

        
    SqlCommand cmd = new SqlCommand("InsertActionType", con);
    cmd.CommandType =CommandType .StoredProcedure ;
    cmd.Parameters.Add("@Aname", SqlDbType.VarChar) = aname ;
        
    SqlDataAdapter  ad=new SqlDataAdapter (cmd);
    DataSet ds=new DataSet ();
        
    con.Open();
    int rows = cmd.ExecuteNonQuery();
    con .Close ();
}

but i recieve 2 error
Error 1 The left-hand side of an assignment must be a variable, property or indexer C:\Documents and Settings\abarsazan.co\My Documents\Visual Studio 2005\WebSites\WebSite12\Default.aspx.cs 24 9 C:\...\WebSite12\
Error 2 Cannot implicitly convert type 'string' to 'System.Data.SqlClient.SqlParameter' C:\Documents and Settings\abarsazan.co\My Documents\Visual Studio 2005\WebSites\WebSite12\Default.aspx.cs 24 59 C:\...\WebSite12\
Posted
Updated 7-Jul-12 18:39pm
v2

1 solution

Change the following line

C#
cmd.Parameters.Add("@Aname", SqlDbType.VarChar) = aname ;


to

C#
cmd.Parameters.Add("@Aname", SqlDbType.VarChar);
cmd.Parameters["@Aname"].Value = aname;


(or)

C#
cmd.Parameters.Add("@Aname", SqlDbType.VarChar).Value = aname;


First you have to add the parameter and specify the type of parameter and then assign a value to that parameter.

Refer this link http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx[^] for more information.
 
Share this answer
 

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