Textbox Auto Number generated in c# error












-2















I am creating a simple product form in c#. while I generated auto number getting error what I tried so far i attached below.product id starts with 00001. I created a method Getproduct() inside the method I wrote the code.error displayed syntax error.



public void Getproduct() {    
string sql;
SqlConnection con = new SqlConnection("server =.; initial catalog=product; integrated security=true");
SqlDataAdapter dr;
SqlDataReader dr1;
sql = "SELECT id,product_name,product_desc,price FROM product Order By id Desc";
SqlCommand com = new SqlCommand(sql, con);
SqlDataReader dr = com.ExecuteReader();
if (dr.Read() == true) {
int id;
int pid;
id = (dr[0] + 1);
pid = id.ToString("00000");
else if IsDBNull(dr) {
pid = ("00001");

}

}

}









share|improve this question




















  • 2





    Please fix curly brackets in the code near else if IsDBNull(dr) - it's something wrong there.

    – stop-cran
    Nov 22 '18 at 7:15











  • id = id = (dr[0] + 1); in this point what is the wrong

    – Programmer Hari
    Nov 22 '18 at 7:18











  • Please describe the problem you faced (a compiler error, a runtime error or something else) and include the error message, exception message+stack trace you got into the question.

    – stop-cran
    Nov 22 '18 at 7:22











  • .error displayed syntax error. id = (dr[0] + 1); at this point

    – Programmer Hari
    Nov 22 '18 at 7:23













  • What do you mean by else? Is it supposed to be related to if (dr.Read() == true)? If yes, add a closing } before else and remove excessive } at the end of the code block.

    – stop-cran
    Nov 22 '18 at 7:25
















-2















I am creating a simple product form in c#. while I generated auto number getting error what I tried so far i attached below.product id starts with 00001. I created a method Getproduct() inside the method I wrote the code.error displayed syntax error.



public void Getproduct() {    
string sql;
SqlConnection con = new SqlConnection("server =.; initial catalog=product; integrated security=true");
SqlDataAdapter dr;
SqlDataReader dr1;
sql = "SELECT id,product_name,product_desc,price FROM product Order By id Desc";
SqlCommand com = new SqlCommand(sql, con);
SqlDataReader dr = com.ExecuteReader();
if (dr.Read() == true) {
int id;
int pid;
id = (dr[0] + 1);
pid = id.ToString("00000");
else if IsDBNull(dr) {
pid = ("00001");

}

}

}









share|improve this question




















  • 2





    Please fix curly brackets in the code near else if IsDBNull(dr) - it's something wrong there.

    – stop-cran
    Nov 22 '18 at 7:15











  • id = id = (dr[0] + 1); in this point what is the wrong

    – Programmer Hari
    Nov 22 '18 at 7:18











  • Please describe the problem you faced (a compiler error, a runtime error or something else) and include the error message, exception message+stack trace you got into the question.

    – stop-cran
    Nov 22 '18 at 7:22











  • .error displayed syntax error. id = (dr[0] + 1); at this point

    – Programmer Hari
    Nov 22 '18 at 7:23













  • What do you mean by else? Is it supposed to be related to if (dr.Read() == true)? If yes, add a closing } before else and remove excessive } at the end of the code block.

    – stop-cran
    Nov 22 '18 at 7:25














-2












-2








-2








I am creating a simple product form in c#. while I generated auto number getting error what I tried so far i attached below.product id starts with 00001. I created a method Getproduct() inside the method I wrote the code.error displayed syntax error.



public void Getproduct() {    
string sql;
SqlConnection con = new SqlConnection("server =.; initial catalog=product; integrated security=true");
SqlDataAdapter dr;
SqlDataReader dr1;
sql = "SELECT id,product_name,product_desc,price FROM product Order By id Desc";
SqlCommand com = new SqlCommand(sql, con);
SqlDataReader dr = com.ExecuteReader();
if (dr.Read() == true) {
int id;
int pid;
id = (dr[0] + 1);
pid = id.ToString("00000");
else if IsDBNull(dr) {
pid = ("00001");

}

}

}









share|improve this question
















I am creating a simple product form in c#. while I generated auto number getting error what I tried so far i attached below.product id starts with 00001. I created a method Getproduct() inside the method I wrote the code.error displayed syntax error.



public void Getproduct() {    
string sql;
SqlConnection con = new SqlConnection("server =.; initial catalog=product; integrated security=true");
SqlDataAdapter dr;
SqlDataReader dr1;
sql = "SELECT id,product_name,product_desc,price FROM product Order By id Desc";
SqlCommand com = new SqlCommand(sql, con);
SqlDataReader dr = com.ExecuteReader();
if (dr.Read() == true) {
int id;
int pid;
id = (dr[0] + 1);
pid = id.ToString("00000");
else if IsDBNull(dr) {
pid = ("00001");

}

}

}






c# sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 7:23







Programmer Hari

















asked Nov 22 '18 at 7:07









Programmer HariProgrammer Hari

578




578








  • 2





    Please fix curly brackets in the code near else if IsDBNull(dr) - it's something wrong there.

    – stop-cran
    Nov 22 '18 at 7:15











  • id = id = (dr[0] + 1); in this point what is the wrong

    – Programmer Hari
    Nov 22 '18 at 7:18











  • Please describe the problem you faced (a compiler error, a runtime error or something else) and include the error message, exception message+stack trace you got into the question.

    – stop-cran
    Nov 22 '18 at 7:22











  • .error displayed syntax error. id = (dr[0] + 1); at this point

    – Programmer Hari
    Nov 22 '18 at 7:23













  • What do you mean by else? Is it supposed to be related to if (dr.Read() == true)? If yes, add a closing } before else and remove excessive } at the end of the code block.

    – stop-cran
    Nov 22 '18 at 7:25














  • 2





    Please fix curly brackets in the code near else if IsDBNull(dr) - it's something wrong there.

    – stop-cran
    Nov 22 '18 at 7:15











  • id = id = (dr[0] + 1); in this point what is the wrong

    – Programmer Hari
    Nov 22 '18 at 7:18











  • Please describe the problem you faced (a compiler error, a runtime error or something else) and include the error message, exception message+stack trace you got into the question.

    – stop-cran
    Nov 22 '18 at 7:22











  • .error displayed syntax error. id = (dr[0] + 1); at this point

    – Programmer Hari
    Nov 22 '18 at 7:23













  • What do you mean by else? Is it supposed to be related to if (dr.Read() == true)? If yes, add a closing } before else and remove excessive } at the end of the code block.

    – stop-cran
    Nov 22 '18 at 7:25








2




2





Please fix curly brackets in the code near else if IsDBNull(dr) - it's something wrong there.

– stop-cran
Nov 22 '18 at 7:15





Please fix curly brackets in the code near else if IsDBNull(dr) - it's something wrong there.

– stop-cran
Nov 22 '18 at 7:15













id = id = (dr[0] + 1); in this point what is the wrong

– Programmer Hari
Nov 22 '18 at 7:18





id = id = (dr[0] + 1); in this point what is the wrong

– Programmer Hari
Nov 22 '18 at 7:18













Please describe the problem you faced (a compiler error, a runtime error or something else) and include the error message, exception message+stack trace you got into the question.

– stop-cran
Nov 22 '18 at 7:22





Please describe the problem you faced (a compiler error, a runtime error or something else) and include the error message, exception message+stack trace you got into the question.

– stop-cran
Nov 22 '18 at 7:22













.error displayed syntax error. id = (dr[0] + 1); at this point

– Programmer Hari
Nov 22 '18 at 7:23







.error displayed syntax error. id = (dr[0] + 1); at this point

– Programmer Hari
Nov 22 '18 at 7:23















What do you mean by else? Is it supposed to be related to if (dr.Read() == true)? If yes, add a closing } before else and remove excessive } at the end of the code block.

– stop-cran
Nov 22 '18 at 7:25





What do you mean by else? Is it supposed to be related to if (dr.Read() == true)? If yes, add a closing } before else and remove excessive } at the end of the code block.

– stop-cran
Nov 22 '18 at 7:25












1 Answer
1






active

oldest

votes


















0














Try this code



public void Getproduct()
{
string sql;
SqlConnection con = new SqlConnection("server =.; initial catalog=product; integrated security=true");
SqlDataAdapter dapt;
SqlDataReader dr1;
sql = "SELECT id,product_name,product_desc,price FROM product Order By id Desc";
SqlCommand com = new SqlCommand(sql, con);
SqlDataReader dr = com.ExecuteReader();

int id;
string pid;

if (dr.Read() == true)
{

int val = 0;

Int32.TryParse(dr[0].ToString(),out val);

id = ( val + 1);
pid = id.ToString("00000");
}
else if( Convert.IsDBNull(dr) )
{
pid = ("00001");

}

}





share|improve this answer
























  • id does not exist in the current context pid does not exist in the current context error displayed like that id = ( val + 1); pid = id.ToString("00000");

    – Programmer Hari
    Nov 22 '18 at 7:30













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425546%2ftextbox-auto-number-generated-in-c-sharp-error%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Try this code



public void Getproduct()
{
string sql;
SqlConnection con = new SqlConnection("server =.; initial catalog=product; integrated security=true");
SqlDataAdapter dapt;
SqlDataReader dr1;
sql = "SELECT id,product_name,product_desc,price FROM product Order By id Desc";
SqlCommand com = new SqlCommand(sql, con);
SqlDataReader dr = com.ExecuteReader();

int id;
string pid;

if (dr.Read() == true)
{

int val = 0;

Int32.TryParse(dr[0].ToString(),out val);

id = ( val + 1);
pid = id.ToString("00000");
}
else if( Convert.IsDBNull(dr) )
{
pid = ("00001");

}

}





share|improve this answer
























  • id does not exist in the current context pid does not exist in the current context error displayed like that id = ( val + 1); pid = id.ToString("00000");

    – Programmer Hari
    Nov 22 '18 at 7:30


















0














Try this code



public void Getproduct()
{
string sql;
SqlConnection con = new SqlConnection("server =.; initial catalog=product; integrated security=true");
SqlDataAdapter dapt;
SqlDataReader dr1;
sql = "SELECT id,product_name,product_desc,price FROM product Order By id Desc";
SqlCommand com = new SqlCommand(sql, con);
SqlDataReader dr = com.ExecuteReader();

int id;
string pid;

if (dr.Read() == true)
{

int val = 0;

Int32.TryParse(dr[0].ToString(),out val);

id = ( val + 1);
pid = id.ToString("00000");
}
else if( Convert.IsDBNull(dr) )
{
pid = ("00001");

}

}





share|improve this answer
























  • id does not exist in the current context pid does not exist in the current context error displayed like that id = ( val + 1); pid = id.ToString("00000");

    – Programmer Hari
    Nov 22 '18 at 7:30
















0












0








0







Try this code



public void Getproduct()
{
string sql;
SqlConnection con = new SqlConnection("server =.; initial catalog=product; integrated security=true");
SqlDataAdapter dapt;
SqlDataReader dr1;
sql = "SELECT id,product_name,product_desc,price FROM product Order By id Desc";
SqlCommand com = new SqlCommand(sql, con);
SqlDataReader dr = com.ExecuteReader();

int id;
string pid;

if (dr.Read() == true)
{

int val = 0;

Int32.TryParse(dr[0].ToString(),out val);

id = ( val + 1);
pid = id.ToString("00000");
}
else if( Convert.IsDBNull(dr) )
{
pid = ("00001");

}

}





share|improve this answer













Try this code



public void Getproduct()
{
string sql;
SqlConnection con = new SqlConnection("server =.; initial catalog=product; integrated security=true");
SqlDataAdapter dapt;
SqlDataReader dr1;
sql = "SELECT id,product_name,product_desc,price FROM product Order By id Desc";
SqlCommand com = new SqlCommand(sql, con);
SqlDataReader dr = com.ExecuteReader();

int id;
string pid;

if (dr.Read() == true)
{

int val = 0;

Int32.TryParse(dr[0].ToString(),out val);

id = ( val + 1);
pid = id.ToString("00000");
}
else if( Convert.IsDBNull(dr) )
{
pid = ("00001");

}

}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 7:26









Sagar ShirkeSagar Shirke

613821




613821













  • id does not exist in the current context pid does not exist in the current context error displayed like that id = ( val + 1); pid = id.ToString("00000");

    – Programmer Hari
    Nov 22 '18 at 7:30





















  • id does not exist in the current context pid does not exist in the current context error displayed like that id = ( val + 1); pid = id.ToString("00000");

    – Programmer Hari
    Nov 22 '18 at 7:30



















id does not exist in the current context pid does not exist in the current context error displayed like that id = ( val + 1); pid = id.ToString("00000");

– Programmer Hari
Nov 22 '18 at 7:30







id does not exist in the current context pid does not exist in the current context error displayed like that id = ( val + 1); pid = id.ToString("00000");

– Programmer Hari
Nov 22 '18 at 7:30




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425546%2ftextbox-auto-number-generated-in-c-sharp-error%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

To store a contact into the json file from server.js file using a class in NodeJS

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen