Hi. I am trying to save details of a person from a form into the SQL server table. It's giving me the following system error: System.Data.SqlClient.SqlException.
I am using a three-tier architecture and there is a DBAS class that features the following function for executing query:
Public Function ConnectToDB() As SqlConnection Dim mCon As New SqlClient.SqlConnection(mstrConStr) mCon.Open() mintNoOfCon = mintNoOfCon + 1 Return mCon End Function Public Function ExecuteQuery(ByVal pstrSQL As String) As Boolean mCon = ConnectToDB() mCom.Connection = mCon mCom.CommandText = pstrSQL * mCom.ExecuteNonQuery()// showing problem :(( mCon.Close() End Function
Can you please tell me what to do? I am a VB.NET beginner. Thanks.
Before executing the query, you should set the m.Com.CommandType to CommandType.Text. However, I'd strongly advise that you use the Microsoft Data Access Application Block (http://www.gotdotnet.com/Community/Workspaces/Workspace.aspx?id=c20d12b0-af52-402b-9b7c-aaeb21d1f431), which provides functionality similar to what you've done, but does so with performance in mind, and encapsulates best practices in accessing and querying database stores using ADO.NET.
It offers SQL execution using a single line of code, for example. What's more, it makes it easier to migrate to other RDBMS products by making the code agnostic to the product -- i.e., you're using SQL Server now, but a customer may want an Oracle database.
Dig Deeper on SQL Server and .NET development
Related Q&A from Daniel Cazzulino
Here Daniel Cazzulino explains how to load a DSL (domain specific language) domain model instance file programmatically. This requires the .NET type ... Continue Reading
Here we offer a glimpse at 12 of .NET development expert Danny Cazzulino's top ASP.NET questions and answers. Continue Reading
C# developers should NOT be modifying InitializeComponent method in the code-behind (or any of the variable definitions) by hand. Continue Reading
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.