I made a stored procedure called "usp_insertempdata" that inserts data into the database table called "Employee", so i executed it and when i save data i brings an exception: "The ConnectionString property has not been initialized. Any help please! here is my code
SqlConnection _SqlConnection = new SqlConnection ( );
string strConnectionString = "";
SqlCommand cmd = new SqlCommand ( );
strConnectionString = @"Server='bisweb\bisweb';Database=x_kgabo;Trusted_Connection=true;";
Int32 rowsAffected;
List<object> lstParams = new List<object> ( );
cmd.Parameters.Add ( new SqlParameter ( "@vchEmployeeNo", SqlDbType.VarChar ) );
cmd.Parameters [ "@vchEmployeeNo" ].Value = txtEmployeeNumber.Text;
cmd.Parameters.Add ( new SqlParameter ( "@vchSurname", SqlDbType.VarChar ) );
cmd.Parameters [ "@vchSurname" ].Value = txtSurname.Text;
cmd.Parameters.Add ( new SqlParameter ( "@vchFirstName", SqlDbType.VarChar ) );
cmd.Parameters [ "@vchFirstName" ].Value = txtFirstName.Text;
cmd.Parameters.Add ( new SqlParameter ( "@vchPosition", SqlDbType.VarChar ) );
cmd.Parameters [ "@vchPosition" ].Value = txtPosition.Text;
cmd.Par开发者_JAVA技巧ameters.Add ( new SqlParameter ( "@vchIdNumber", SqlDbType.VarChar ) );
cmd.Parameters [ "@vchIdNumber" ].Value = txtIdNumber.Text;
cmd.Parameters.Add ( new SqlParameter ( "@vchGender", SqlDbType.VarChar ) );
cmd.Parameters [ "@vchGender" ].Value = drpdwnGender.SelectedItem.Value;
cmd.Parameters.Add ( new SqlParameter ( "@vchRace", SqlDbType.VarChar ) );
cmd.Parameters [ "@vchRace" ].Value = drpdwnRace.Text;
cmd.Parameters.Add ( new SqlParameter ( "@vchEmail", SqlDbType.VarChar ) );
cmd.Parameters [ "@vchEmail" ].Value = txtEmail.Text;
cmd.Parameters.Add ( new SqlParameter ( "@iBranchNo", SqlDbType.VarChar ) );
cmd.Parameters [ "@iBranchNo" ].Value = txtBranchNo.Text;
cmd.Parameters.Add ( new SqlParameter ( "@bHasMentor", SqlDbType.VarChar ) );
cmd.Parameters [ "@bHasMentor" ].Value = drpdwnMentor.Text;
cmd.Parameters.Add ( new SqlParameter ( "@vchMentorName", SqlDbType.VarChar ) );
cmd.Parameters [ "@vchMentorName" ].Value = txtMentorName.Text;
cmd.Parameters.Add ( new SqlParameter ( "@vchMentorStaffNo", SqlDbType.VarChar ) );
cmd.Parameters [ "@vchMentorStaffNo" ].Value = txtMentorStaffNo.Text;
cmd.Parameters.Add ( new SqlParameter ( "@dtMentorDate", SqlDbType.VarChar ) );
cmd.Parameters [ "@dtMentorDate" ].Value = calMentorDate.SelectedDate;
cmd.CommandText = "usp_insertempdata";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = _SqlConnection;
_SqlConnection.Open ( );
rowsAffected = cmd.ExecuteNonQuery ( );
_SqlConnection.Close ( );
try
{
_SqlConnection.ConnectionString = strConnectionString;
_SqlConnection.Open ( );
}
catch ( Exception ex )
{
//throw ex;
messagefunctions ( );
lblError.Text = "There was a problem inserting a record into the database, please consult with systems administrators " + ex.Message.ToString ( );
}
finally
{
if ( _SqlConnection != null )
_SqlConnection.Close ( );
}
You're setting your connection string:
strConnectionString = @"Server='bisweb\bisweb';Database=x_kgabo;Trusted_Connection=true;";
(btw: I believe you shouldn't be using any single quotes around the server name)
but you're never assigning it to your SqlConnection
!
Change your code around so that the connection string will actually be used in your SqlConnection
object! Also - always put your SqlConnection
and SqlCommand
into using(....) { .... }
blocks....
string strConnectionString = @"Server=bisweb\bisweb;Database=x_kgabo;Trusted_Connection=true;";
string storedProcName = "usp_insertempdata";
using(SqlConnection _SqlConnection = new SqlConnection(strConnectionString))
using(SqlCommand cmd = new SqlCommand(storedProcName, _SqlConnection))
{
cmd.CommandType = CommandType.StoredProcedure;
Int32 rowsAffected;
......
}
The problems seems to be that you are assigning the connection string after you called ExecuteNonQuery and after your first call to Open. The second call is nonsense.
The correct sequence would be:
cmd.CommandType = CommandType.StoredProcedure;
_SqlConnection.ConnectionString = strConnectionString;
cmd.Connection = _SqlConnection;
try
{
_SqlConnection.Open ( );
rowsAffected = cmd.ExecuteNonQuery ( );
_SqlConnection.Close ( );
}
精彩评论