Troubleshooting C# SQL insert


Troubleshooting C# SQL insert



Up until a few hours ago, this code and similar code worked fine. I dropped and recreated the database to change the timestamp datatype so it would show as the time the record was updated. That is fine now, I've used current_timestamp to fix it.


current_timestamp



That was the ONLY change I made to the database, otherwise it's the same server, same database name, same connection string in VS2017. I even changed servers a few days ago and nothing went wrong but for some reason this code hasn't worked since I dropped it.



Can anyone suggest why? Was the code just originally flawed?


protected void BtnSubmitPost_ServerClick(object sender, EventArgs e)
{
string bpTitle = txtBlogHeader.Text;
string bpBody = txtBlogBody.Text;

using (SqlConnection addBlogConn = new SqlConnection(ConfigurationManager.ConnectionStrings["childrens"].ConnectionString))
{
using (SqlCommand addBlogCmd = new SqlCommand())
{
try
{
addBlogCmd.CommandType = CommandType.Text;
addBlogCmd.CommandText = "INSERT INTO blogPosts (bp_header, bp_body) VALUES (@bpTitle, @bpBody)";

addBlogCmd.Parameters.Add("@bpTitle", SqlDbType.VarChar).Value = bpTitle;
addBlogCmd.Parameters.Add("@bpBody", SqlDbType.VarChar).Value = bpBody;
addBlogCmd.Connection = addBlogConn;

addBlogConn.Open();
lblBhError.Visible = false;
//lblStaffConfirm.Visible = true;
lvBlogs.DataBind();
addBlogConn.Close();
}
catch
{
Response.Redirect("/staff.aspx");
}
}
}
}





Can you add the error you are encountering, what line it manifests and the data that you are passing in, please?
– JayV
Jul 2 at 21:30





If you dropped and re-added the database, did you remember to re-adjust the permissions? It's possible your database user is orphaned from your login.
– Dave.Gugg
Jul 2 at 21:32





It's not an error exactly, it's just not inserting data, even though it is reading data from the db. I've inserted breakpoints and it breaks ALLL the way through
– Shakira Rhiane Salazar
Jul 2 at 21:34





Please remove/comment out the try/catch and see if it throws an exception.
– Lasse Vågsæther Karlsen
Jul 2 at 21:42





Also, where are you actually executing the sql?
– Lasse Vågsæther Karlsen
Jul 2 at 21:42




1 Answer
1



Well I don't see you are executing the command addBlogCmd at all since there is nothing after addBlogConn.Open(); statement. Moreover, neither I see you are using disconnected architecture by means of dataadapter/dataset. So probably that explains the problem.


addBlogCmd


addBlogConn.Open();



Essentially you are missing addBlogCmd.ExecuteNonQuery() statement to execute the command you have prepared


addBlogCmd.ExecuteNonQuery()





I'll add that back and see if it works but I took it away because when the code was working it was causing a duplicate entry to the db
– Shakira Rhiane Salazar
Jul 2 at 21:36





re-adding addBlogCmd.ExecuteNonQuery(); works but it is still adding duplicate entries
– Shakira Rhiane Salazar
Jul 2 at 21:51






Thanks so much Rahul, I know that was a rookie mistake but I'm glad you took the time, I also fixed why it was giving me duplicates.
– Shakira Rhiane Salazar
Jul 2 at 22:17






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages