Fahmieyz
Fahmieyz

Reputation: 255

Why I can't update my SQL Server table using this code?

Currently, I been doing a programming for a website using asp.net that extract and update data from Microsoft SQL Server.

Right now I need to update certain value in certain column in certain table, but my update SqlCommand is not working. I already do some researched around the internet but it is not working. There are no error, but the value in SQL server table in column WeightageFactor on row Criticality didn't update when I click the button. I use this SQL query and it works just fine:

UPDATE dbo.HullPoF 
SET WeightageFactor = 0.10 
WHERE Criticality = 'Fatigue Loading'

This is my code:

protected void btnSubmitPoF_Click(object sender, EventArgs e)
{
    conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["myDBconn1"].ConnectionString);

    commPoF = new SqlCommand("UPDATE dbo.HullPoF SET WeightageFactor = @WF WHERE Criticality='Fatigue Loading'", conn1);
    commPoF.Parameters.Add("@WF", System.Data.SqlDbType.NVarChar).Value = txtWFFatLoad.Text; ;

    conn1.Open();
    commPoF.ExecuteNonQuery();
    conn1.Close();
}

What is wrong with my code? One of the solution I try to use is from Update table using SqlCommand in asp.net

The current value is 0.20 and I want to change to 0.10

p/s: This is my first question in this website. If my question format is wrong please tell me.

Upvotes: 1

Views: 1143

Answers (4)

Fahmieyz
Fahmieyz

Reputation: 255

After reviewed back through hundreds of code line in project especially in Page_Load, it seem the problem was something to do with Post Back function.

All I have to do is to put

if(!IsPostBack) { }

to some of my code in Page_Load

Sorry to waste everyone time with my silly mistake.

Upvotes: 1

mohsen
mohsen

Reputation: 1806

If WeightageFactor is double so convert it and send it to parameters

double WF = double.Parse( txtWFFatLoad.Text);
commPoF.Parameters.Add("@WF", SqlDbType.Float);
commPoF.Parameters["@WF"].Value = WF;

If WeightageFactor is string so Use

commPoF.Parameters.AddWithValue("@WF",txtWFFatLoad.Text);

Upvotes: 1

Mihir Kale
Mihir Kale

Reputation: 1118

replace

commPoF.Parameters.Add("@WF", System.Data.SqlDbType.NVarChar).Value = txtWFFatLoad.Text;

with

commPoF.Parameters.AddWithValue("@WF", txtWFFatLoad.Text);

Upvotes: 1

G.Emiri
G.Emiri

Reputation: 11

I replicated your problem and the syntax is correct. Check the connection string.

Upvotes: 1

Related Questions