Last post Nov 18, 2010 10:41 AM by ramesh866
Nov 18, 2010 12:46 AM|priyananth|LINK
Is there a way to configure SqlDatasource to check for a row and insert the data.
Simply in user table if username is exist return some values other wise insert. User should be able to know whether the record is saved or not.
Without using stored procedure.
Nov 18, 2010 01:23 AM|Ahmed Moosa|LINK
try to use something like this :
InsertCommand="IF NOT EXISTS (Select some from some table ) BEGIN INSERT (some ) VALUES (@some) END"
hope this helps
Nov 18, 2010 01:23 AM|santa_1975|LINK
You can have an InsertCommand like the one below and pass the appropriate parameters to first check and then insert if the record doesn't exist.
InsertCommand="IF NOT EXISTS (SELECT * FROM Users WHERE UserName = @UserName) BEGIN INSERT [Users] ([UserName], [FirstName], [LastName], [City], [State], [Country]) VALUES (@UserName, @FirstName, @LastName, @City, @State, @Country) END"
Hope this helps.
Nov 18, 2010 10:13 AM|priyananth|LINK
If the record is already exist I should show a Message.. How it can be identified?
Nov 18, 2010 10:41 AM|ramesh866|LINK
am sure that you have must be having a primary key column in your table.. so when try you insert dupilcate values there will be an error thrown called violation of primary key... so in the insert statement use the try catch block like this
catch ( SqlException ex)
if (ex.Message.StartsWith("Violation of PRIMARY KEY constraint"))
/// your error message