Input string was not in a correct format(windows applications)?

Posted by Sudheep.grandhe under C# on 11/30/2011 | Points: 10 | Views : 3256 | Status : [Member] | Replies : 6
Hi,

in my coding i found this error.please do the needful.

private void UpdateCustomerName_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("data source=mars-104;initial catalog=marsweb;integrated security=true");
selectQueryString = "select * from tbl_customer";
conn.Open();
sqlDataAdapter = new SqlDataAdapter(selectQueryString, conn);
sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
dataTable = new DataTable();
int StartRec = Convert.ToInt32(txtstartrec.Text);//error:input string was not in a correct format.
int MaximumRec = Convert.ToInt32(txtmaxrec.Text);
sqlDataAdapter.Fill(startrec,maximumrec,dataTable );
bindingSource = new BindingSource();
bindingSource.DataSource = dataTable;
}

Best,
Sudheep.



Responses

Posted by: Vuyiswamb on: 11/30/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
In which line does it happen ?


Thank you for posting at Dotnetfunda
[Administrator]

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksingh on: 11/30/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

Change your below code

int StartRec = Convert.ToInt32(txtstartrec.Text);

as
if(txtstartrec.Text.Trim() != "")

int StartRec = Convert.ToInt32(txtstartrec.Text);


Hope it will solve your problem.

Regards,
Sunil

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Arefin on: 11/30/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

You can use Int32.TryParse() method to avoid this kind of error.

Thanks,
Arefin

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 11/30/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

It shows this Error1:Embedded statement cannot be a declaration or labeled statement
if (txtstartrec .Text .Trim ()!="")
int StartRec = Convert.ToInt32(txtstartrec.Text);
int MaxmiumRec = Convert.ToInt32(txtmaxrec.Text);
sqlDataAdapter.Fill(dataTable);

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksingh on: 12/1/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

Dont declare any variable inside if block
int StartRec;

if (txtstartrec.Text .Trim ()!="") 

{
StartRec = Convert.ToInt32(txtstartrec.Text);
}
int MaxmiumRec = Convert.ToInt32(txtmaxrec.Text);


Try this ....

Regards,
Sunil

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Samanthajyesta on: 12/1/2011 [Member] Starter | Points: 25

Up
0
Down
Hi.
I hope error is in this line.

sqlDataAdapter.Fill(startrec, maximumrec, dataTable);
here you have to pass the parameters properly. means you can pass like,

sqlDataAdapter.Fill(dataTable);

This is the solution for your issue.
Hope this is helpful.

Cheers,
Samantha.

http://www.dotnetfunda.com

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response