Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i did not get any name which is available in database for comparing the given textbox data,so please tell me how to get data from database in order to comparing the i/p data for avoiding duplicates

What I have tried:

vb.net code
<WebMethod()> _
Public Shared Sub AddRecord(FName As String)

Dim con As New SqlConnection("Data Source=;Database=;User Id=;Password=")
Try
Dim dt As New DataTable
Dim cmd2 As New SqlCommand("INSERT MDT_MOBILE_FEATURES_SETTINGS(FEATURES_NAME) VALUES(@Name)")
cmd2.Parameters.AddWithValue("@Name", FName)
Dim cmd As New SqlCommand("select FEATURES_NAME from MDT_MOBILE_FEATURES_SETTINGS")

cmd.Connection = con

Dim da As New SqlDataAdapter(cmd)
da.Fill(dt)
Dim strFName(dt.Rows.Count - 1) As String
Dim X As String

Dim a As Integer
Dim i As Integer
strFName(i) =
For i = 0 To dt.Rows.Count - 1

If (strFName(i) = X) Then

End If

Next


strFName(a) = (a)
a = a + 1



cmd2.Connection = con
con.Open()

cmd2.ExecuteNonQuery()

Catch ex As Exception
Throw



End Try
End Sub
Posted
Updated 2-Jan-17 1:07am
Comments
Michael_Davies 2-Jan-17 6:51am    
Not sure what you are asking but:

Dim cmd2 As New SqlCommand("INSERT MDT_MOBILE_FEATURES_SETTINGS(FEATURES_NAME) VALUES(@Name)")

Should be:

Dim cmd2 As New SqlCommand("INSERT INTO MDT_MOBILE_FEATURES_SETTINGS (FEATURES_NAME) VALUES(@Name)")

Normally you wild test a field value with a select to see if it already exists before inserting if you do not want duplicates.
Devaraneni Laxman Rao 2-Jan-17 23:51pm    
"into" is optional in insert command in mssql

1 solution

Hi Devaraneni,

You should check if the feature name existing in the database before inserting a new row.

If the feature name does not exist then only a new row should be inserted else an exception should be thrown and an error should be displayed in the UI.

I hope following code might help you.

VB
Public Sub AddRecord(featureName As String)
	Dim connection = New SqlConnection("Data Source=;Database=;User Id=;Password=")
	Dim sqlCmd = New SqlCommand("select Top 1 FEATURE_NAME from MDT_MOBILE_FEATURES_SETTINGS WHERE FEATURES_NAME = @featureName")

	sqlCmd.Parameters.Add(New SqlParameter("@featureName", featureName))

	sqlCmd.CommandType = CommandType.Text

	Dim result = sqlCmd.ExecuteScalar()

	If result IsNot Nothing Then
		Throw New Exception(String.Format("Feature name {0} already exist.", featureName))
	End If

	Dim sqlInsertCommand = New SqlCommand("INSERT MDT_MOBILE_FEATURES_SETTINGS(FEATURES_NAME) VALUES(@Name)")

	sqlInsertCommand.Parameters.AddWithValue("@Name", featureName)

	sqlInsertCommand.ExecuteNonQuery()

	connection.Close()
End Sub


Thanks and regards,
Chetan Ranpariya
 
Share this answer
 
Comments
Devaraneni Laxman Rao 2-Jan-17 23:53pm    
console.write("ex.message") is not print output msg please tell me how to display error
Chetan Ranpariya 4-Jan-17 10:48am    
Hi Devaraneni,

What kind of application you are developing? Is is a Console Application, Windows Application or Web Application?

You won't see the output at Console if the application is a Windows Application or Web Application.
Devaraneni Laxman Rao 4-Jan-17 22:47pm    
Web Application
Devaraneni Laxman Rao 16-Jan-17 22:32pm    
in web method how can i show error messages

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