C# .NET - exception
Asked By Mackvin Pereira
17-Jan-12 12:59 PM
string StrConnectionString = ConfigurationManager.AppSettings["MyDbConn1"].ToString();
SqlConnection conn = new SqlConnection(StrConnectionString);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
SqlCommand command = new SqlCommand("spToInsertUserinfo", conn);
cmd.Parameters.Add(
new SqlParameter("@FristName", txtFristName.Text));
cmd.Parameters.Add(new SqlParameter("@LastName", txtLastName.Text));
cmd.Parameters.Add(new SqlParameter("@Gender", gen));
cmd.Parameters.Add(new SqlParameter("@add", txtAddr.Text));
cmd.Parameters.Add(new SqlParameter("@ContactNo", txtContactNo.Text));
cmd.Parameters.Add(new SqlParameter("@UserName", txtUserName.Text));
cmd.Parameters.Add(new SqlParameter("@Password", txtPss.Text));
conn.Open();
cmd.ExecuteNonQuery();
throwing execption "{"ExecuteNonQuery: CommandText property has not been initialized"}"
please help
Robbe Morris replied to Mackvin Pereira
This looks like a spelling error:
SqlParameter("@FristName", txtFristName.Text));
Also, you are creating two command objects. cmd and command. You should only have one. You are using "cmd" in parts of your code and "command" in other parts.
dipa ahuja replied to Mackvin Pereira
This is because you have created the object of the sql command without new keyword and called the CreateCommand Method
Remove the CreateCommand there is no need of it , change your code to this..
string StrConnectionString = ConfigurationManager.AppSettings["MyDbConn1"].ToString();
SqlConnection conn = new SqlConnection(StrConnectionString);
SqlCommand command = new SqlCommand("spToInsertUserinfo", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@FristName", txtFristName.Text));
[)ia6l0 iii replied to Mackvin Pereira
Comment the CreateCommand statement and also place the commandtype statement after intializing the command.
string StrConnectionString = ConfigurationManager.AppSettings["MyDbConn1"].ToString();
SqlConnection conn = new SqlConnection(StrConnectionString);
//SqlCommand cmd = conn.CreateCommand();
SqlCommand command = new SqlCommand("spToInsertUserinfo", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@FristName", txtFristName.Text));
cmd.Parameters.Add(new SqlParameter("@LastName", txtLastName.Text));
cmd.Parameters.Add(new SqlParameter("@Gender", gen));
cmd.Parameters.Add(new SqlParameter("@add", txtAddr.Text));
cmd.Parameters.Add(new SqlParameter("@ContactNo", txtContactNo.Text));
cmd.Parameters.Add(new SqlParameter("@UserName", txtUserName.Text));
cmd.Parameters.Add(new SqlParameter("@Password", txtPss.Text));
conn.Open();
cmd.ExecuteNonQuery();
However, you should also be using "using" blocks in this code.
Hope that helps.
D Company replied to Mackvin Pereira
hello friend,
this exception is because of 2 possible reason
1. check your sql query
2.check your connection string in app.config how you have define it, if you have define it like this
<configuration>
<appSettings>
<add key="Destination" value="ToSQL"/>
<add key="Sever" value="Data Source=MYSQLSEVER ;Initial Catalog=dbname;User Id=id;Password=pwd"/>
</appSettings>
</configuration>
change it as
<configuration>
<connectionStrings>
<add name="MyDBConnectionString" providerName="System.Data.SqlClient"
connectionString="Data Source=localhost;Initial Catalog=dbname; Integrated Security=true" />
</connectionStrings>
</configuration>
Hope this will help you
Regards
D
D Company replied to Mackvin Pereira
hello friend,
Oh!
the mistake is in your initialization itself
replace your code with this surely will work
string StrConnectionString = ConfigurationManager.AppSettings["MyDbConn1"].ToString();
SqlConnection conn = new SqlConnection(StrConnectionString);
SqlCommand cmd = new SqlCommand("spToInsertUserinfo", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(
new SqlParameter("@FristName", txtFristName.Text));
cmd.Parameters.Add(new SqlParameter("@LastName", txtLastName.Text));
cmd.Parameters.Add(new SqlParameter("@Gender", gen));
cmd.Parameters.Add(new SqlParameter("@add", txtAddr.Text));
cmd.Parameters.Add(new SqlParameter("@ContactNo", txtContactNo.Text));
cmd.Parameters.Add(new SqlParameter("@UserName", txtUserName.Text));
cmd.Parameters.Add(new SqlParameter("@Password", txtPss.Text));
conn.Open();
cmd.ExecuteNonQuery();
Regards
D

array of SqlParameters to be added to command< / param> private static void AttachParameters(SqlCommand command, SqlParameter[] commandParameters) { if( command = = null ) throw new ArgumentNullException( "command" ); if( commandParameters ! = null ) { foreach (SqlParameter p in commandParameters) { if( p ! = null ) { / / Check for derived output value with no value assigned dataRow used to hold the stored procedure's parameter values< / param> private static void AssignParameterValues(SqlParameter[] commandParameters, DataRow dataRow) { if ((commandParameters = = null) | | (dataRow = = null)) { / / Do nothing if we get no data return; } int i = 0; / / Set the parameters values foreach(SqlParameter commandParameter in commandParameters) { / / Check the parameter name if( commandParameter.ParameterName = = null | | commandParameter.ParameterName.Length < = 1 parameterValues"> Array of objects holding the values to be assigned< / param> private static void AssignParameterValues(SqlParameter[] commandParameters, object[] parameterValues) { if ((commandParameters = = null) | | (parameterValues = = null)) { / / Do nothing if we get no data summary> / / / <param name = "command"> The SqlCommand to be prepared< / param> / / / <param name = "connection"> A valid SqlConnection, on which to execute this command< / param> / / / <param name = "transaction"> A valid SqlTransaction, or 'null was opened by the method, otherwose is false.< / param> private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, out bool mustCloseConnection ) { if( command = = null ) throw new ArgumentNullException( "command" ); if( commandText = = null | | commandText
connection string error try { string StrConnectionString = ConfigurationManager.AppSettings["MyDbConn1"].ToString(); SqlConnection conn = new SqlConnection(StrConnectionString); SqlCommand cmd = conn.CreateCommand(); cmd.CommandType = CommandType.StoredProcedure; SqlCommand command = new SqlCommand("spToInsertUserinfo", conn); cmd.Parameters.Add( new SqlParameter("@FristName", txtFristName.Text)); cmd.Parameters.Add(new SqlParameter("@LastName", txtLastName.Text)); cmd.Parameters.Add(new SqlParameter("@Gender", gen)); cmd.Parameters.Add(new SqlParameter("@add", txtAddr.Text)); cmd.Parameters.Add(new SqlParameter("@ContactNo", txtContactNo.Text)); cmd.Parameters.Add(new SqlParameter("@UserName", txtUserName.Text)); cmd.Parameters.Add(new SqlParameter("@Password", txtPss.Text)); conn.Open(); cmd.ExecuteNonQuery
size of musicid ok? public static void Insert( int ProfileID, int MusicID, string connectionString) { using ( SqlConnection connection = new SqlConnection (System.Configuration. ConfigurationManager .ConnectionStrings[ "omegaloveConnectionString" ].ConnectionString)) { SqlParameter theParam = new SqlParameter ( "@ProfileID" , SqlDbType .Int, 8, "ProfileID" ); SqlParameter theParam1 = new SqlParameter ( "@MusicID" , SqlDbType .TinyInt, 4, "MusicID" ); theParam.Value = ProfileID; theParam1.Value = MusicID; theSQLCommand.Parameters.Add(theParam theSQLCommand.Parameters.Add(theParam1); theSQLCommand.Connection.Open(); theSQLCommand.ExecuteNonQuery(); theSQLCommand.Connection.Close(); } } create proc sprInsertIntoMusicXProfile ( @ProfileId int , @MusicId tinyint ) as begin if not exists theSQLCommand. CommandText = "usp_InsertValues"; / / Store procedure's name here. . . . . . . theSQLCommand. .CommandType = System.Data.CommandType.StoredProcedure; theSQLCommand.ExecuteNonQuery(); theSQLCommand.Connection.Close(); Couple of things. . You need to specify the stored procedure name to
it goes. . . private void SaleSave_Click(object sender, EventArgs e) { if (SaleTotalTran.SelectedIndex = = 0) { con = new SqlConnection("Data Source = RISHI-PC \ RISHI; Initial Catalog = retail; User ID = sa; Password = helloworld"); con.Open Qty6, @Pcs6, @Unit6, @Rate6, @Amt6, @DiscAmt, @StAmt, @VatAmt, @AVatAmt, @NetTotal)", con); cmd4.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar)); cmd4.Parameters.Add(new SqlParameter("@Address", SqlDbType.VarChar)); cmd4.Parameters.Add(new SqlParameter("@VNo", SqlDbType.Int)); cmd4.Parameters.Add(new SqlParameter("@VDate", SqlDbType.DateTime)); cmd4.Parameters.Add(new SqlParameter("@BillNo", SqlDbType.Int)); cmd4.Parameters.Add(new SqlParameter("@Remark", SqlDbType.VarChar)); cmd4.Parameters.Add(new SqlParameter("@ItemName1", SqlDbType.VarChar)); cmd4.Parameters.Add(new SqlParameter("@Qty1", SqlDbType.Float)); cmd4.Parameters.Add(new