TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
gerald vince
NA
15
7.2k
how do I check if an item exists before inserting into DB
Feb 4 2015 4:50 PM
I am trying to check if there is a project name already in the database, but I cannot get it to work, I have the insert working just fine, so I commented out what I was trying to work with(not working)
here is my C#
protected void CreateProject_Click(object sender, EventArgs e)
{
var insCmd = new SqlCommand("INSERT INTO Projects (TesterName, ProjectName, ProjectDescription, DueDate, DateAssigned, Platform) values (@TesterName, @ProjectName,@ProjectDesc, @DueDate, @DateAssigned, @Platform)");
insCmd.Parameters.AddWithValue("@ProjectName", txtProjectName.Text);
insCmd.Parameters.AddWithValue("@ProjectDesc", txtProjectDesc.Text);
insCmd.Parameters.AddWithValue("@TesterName", ddlTester.SelectedValue);
insCmd.Parameters.AddWithValue("@DateAssigned", txtStartDate.Text);
insCmd.Parameters.AddWithValue("@DueDate", txtEndDate.Text);
insCmd.Parameters.AddWithValue("@Platform", TxtProjectPlatform.Text);
//if (!projId())
//{ }
//else
//{
using (var cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["ProjectsAndTasksTestConnectionString"].ConnectionString))
{
insCmd.Connection = cnn;
cnn.Open();
insCmd.ExecuteNonQuery();
cnn.Close();
}
//}
ClientScript.RegisterStartupScript(this.GetType(), "insert-success", "alert('Project created');", true);
}
/*public bool projID()
{
using (var cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["ProjectsAndTasksTestConnectionString"].ConnectionString))
{
cnn.Open();
string query = "Select ProjectdName FROM Projects WHERE ProjectName= '" + txtProjectName.Text + "'";
SqlCommand cmd = new SqlCommand(query, cnn);
SqlDataReader dr;
dr = cnn.ExecuteReader();
while (dr.Read())
{
projID = dr["projID"].ToString();
if (projID != "0")
{
ClientScript.RegisterStartupScript(this.GetType(), "insert-fail", "alert('Project Name Already Exists');", true);
return false;
}
cnn.Close();
}
return true;
}
}*/
Reply
Answers (
4
)
Scheduling/Event app
how to create setup file with database .mdf file(vs2010)