-1

i was programming an update statement on c# and SQL Database, for my grad project, i had everything going pretty fine and smooth until i faced a weird problem, once i finished my update statement, and was setting an if condition to catch the error, the msg string is giving me weird error

use of unassigned local variable

however here's the code below.

string msg;
if (MessageBox.Show("Are you sure you want to update book info?", "Updating", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
    n.RunDml("Update into Book Values (" + txtID.Text + ",'" + txtName.Text + "','" + txtCond.Text + "','" + txtQuant.Text + "','" + txtSect.Text + "')");
    if (msg == "ok")
    {
        MessageBox.Show("Updating successfully done! ", "Updating");
    }
    else
    {
        MessageBox.Show(msg);
    }
}

any help? thanks.

Igor
  • 60,821
  • 10
  • 100
  • 175
M.Morgan
  • 13
  • 5
  • 1
    You're using an unassigned local variable. Stop doing so. (The use is with `if (msg == "ok")`, because you never assign a value to `msg`. It's unitialized when you use it in the above if statement. You can't do that; you can't ever use uninitialized variables.) Where do you expect the magic to happen between `string msg;` and `if (msg ==` that would somehow make it equal to `"ok"`? (Your update statement is incorrect, too. That syntax is invalid. Google *c sharp parameterized queries*.) – Ken White Feb 22 '17 at 19:05
  • what IDE are you using? It should show you exactly what and where. – Crowcoder Feb 22 '17 at 19:07
  • i used it on Insert statement and it goes okay, what is the difference, i still a beginner after all.. @KenWhite – M.Morgan Feb 22 '17 at 19:18
  • @Usman i have seen this topic before, didn't help me that much.. – M.Morgan Feb 22 '17 at 19:19
  • @Crowcoder sorry, IDE? – M.Morgan Feb 22 '17 at 19:19
  • what is the use of string msg? – Usman Feb 22 '17 at 19:22
  • and can you show code of n.RunDml – Usman Feb 22 '17 at 19:23
  • Integrated Development Environment. For instance, Visual Studio would red squiggly underline `msg`. – Crowcoder Feb 22 '17 at 19:23

1 Answers1

1

No where in your code are you setting a value for msg so you can't do a check to see if its equal to "ok", that is what the error is stating.

Maybe you meant to do this?

msg = n.RunDml("Update into Book Values (" + txtID.Text + ",'" + txtName.Text + "','" + txtCond.Text + "','" + txtQuant.Text + "','" + txtSect.Text + "')");

But that depends on when you expect to assign a value to msg

Igor
  • 60,821
  • 10
  • 100
  • 175
  • the point i wanna make, is to show up to me whatever error will happen, i used it in insert statement, and it goes pretty ok without any errors, i just wondering .. – M.Morgan Feb 22 '17 at 19:17
  • @M.Morgan - that comment made no sense... Read my answer and everyone's comments above again. The problem is `msg`, you declare it but never assign a value and until you do assign a value any comparison of that instance will raise an error. – Igor Feb 22 '17 at 19:24
  • Oh! am really sorry! i just noticed now! my bad! am very sorry!, thanks for your notice, really appreciated as well! :) – M.Morgan Feb 22 '17 at 19:27