Click here to Skip to main content
12,693,693 members (35,757 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ass3cs
    public partial class show_database : Form
        public show_database()
        private void show_database_Load(object sender, EventArgs e)
            SqlConnection dbconnection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\edu\\7th sem\\\\assignment\\ass3cs\\ass3cs\\BikeData.mdf;Integrated Security=True;User Instance=True");
            SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM customer_database", dbconnection);

            DataTable dt = new DataTable();
            GridView.DataSource = dt;

        private void close_btn_Click(object sender, EventArgs e)

        private void GridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
            Int id = Convert.ToInt32(GridView.Rows[e.RowIndex].Cells[0].Value);
            UpdateDatabase ud = new UpdateDatabase(id);
Posted 11-Oct-12 10:10am
Updated 11-Oct-12 10:18am
Wes Aday 11-Oct-12 15:17pm
And the value of the string when the error is generated is what? Use Int32.Parse or TryParse instead of Convert.
NishTrivedi 11-Oct-12 16:04pm
Input string is not in correct format.
and sorry..
TryParse does not work.
Wes Aday 11-Oct-12 16:11pm
"does not work" is not very helpful in describing a problem. TryParse does work. And "Input string is not in correct format" is not an answer. What is the value of the string that you are trying to convert?
NishTrivedi 11-Oct-12 16:15pm
i want to get the cell number while i click on that cell.
and i want to convert string value into integer value.
Wes Aday 11-Oct-12 16:18pm
Believe it or not I understand that already. What is the string that you are trying to convert? It's a simple question...
NishTrivedi 11-Oct-12 16:20pm
string contains name value.
Wes Aday 11-Oct-12 16:22pm
And what exactly does that mean? You mean a string containing a name like "Bob"?
NishTrivedi 11-Oct-12 16:17pm
when i use TryParse at that time "No overload for method 'TryParse' takes 1 argument is generate.
Wes Aday 11-Oct-12 16:19pm
And? So you did not use the function properly. That does not mean the function does not work. That means that you did not use it correctly.
NishTrivedi 11-Oct-12 16:22pm
Int id = Convert.ToInt32(GridView.Rows[e.RowIndex].Cells[0].Value);

so can you tell me how i use TryParse in this code line??
Wes Aday 11-Oct-12 16:28pm
Yes, although I fail to see why you cannot read the documentation yourself.
int t;
Int32.TryParse(GridView.Rows[e.RowIndex].Cells[0].Value, out t);
NishTrivedi 11-Oct-12 16:56pm
Argument 1: cannot convert from 'object' to 'string' error generate.
Wes Aday 11-Oct-12 17:02pm
Then try
Int32.TryParse(GridView.Rows[e.RowIndex].Cells[0].Value.ToString(), out t);
NishTrivedi 11-Oct-12 17:08pm
exception:conversion failed when converting the varchar value 'xyz' to datatype int.
NishTrivedi 11-Oct-12 17:11pm

i want to perform this task.
Wes Aday 11-Oct-12 17:12pm
And? Did you expect something else? Do you think that "xyz" should be an int somehow?
Wes Aday 11-Oct-12 17:17pm
And that is an exception from your database.
NishTrivedi 12-Oct-12 8:58am
ridoy 11-Oct-12 15:51pm
try with int place of Int
NishTrivedi 11-Oct-12 16:05pm
no change..
still same error is generate.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If you are going to use the Convert.ToInt32 method then you need to wrap that call in a try-catch so that you can capture and deal with that exception.
The better and more preferred ways would be to use the Int32.Parse or TryParse methods as Wes has alluded to in his comment.

The basic concept to learn here is that all user input should be treated as though it is evil. Validate the hell out of it and make sure that you can catch as many eventualities as you can and deal with them appropriately.

[Update]: The reasons for the exception to occur in the Convert.ToInt32 scenario are plenty. If the textbox value is empty or if the textbox value contains anything other than just 0 through 9 then this exception will occur. The idea of the try catch is to provide a graceful handling of the error.
int valueToUse;
   valueToUse = Convert.ToInt32(GridView.Rows[e.RowIndex].Cells[0].Value);
   // I suspect that you may want to use .Text instead of .Value here
   // valueToUse = Convert.ToInt32(GridView.Rows[e.RowIndex].Cells[0].Text);
catch( Excetion error )
   // Set a default value so that you can go on.
   valueToUse = 0;

The preferred way would be to use the TryParse though.
int valueToUse;
// TryParse returns a boolean indicating whether the conversion was successful.
if( !Int32.TryParse( GridView.Rows[e.RowIndex].Cells[0].Value, out valueToUse ) )
   // Set a default value.
   valueToUse = 0;

  // Notify the user or do whatever else you want.
NishTrivedi 11-Oct-12 15:53pm
hi Marcus,
still the same error is generated using convet.toint32 in try-catch..
Marcus Kramer 11-Oct-12 16:35pm
Of course the error will still occur because the input in the textbox cannot be converted into a number. The try-catch will just give you the opportunity of applying a default value or letting the user know in a graceful way.
NishTrivedi 11-Oct-12 16:54pm
Argument 1: cannot convert from 'object' to 'string'
error generate.
Marcus Kramer 11-Oct-12 16:56pm
Show the 1 line of code, please?
NishTrivedi 11-Oct-12 16:57pm
this line creates the error:
Marcus Kramer 11-Oct-12 17:08pm
Try GridView.Rows[e.RowIndex].Cells[0].Value.ToString() or GridView.Rows[e.RowIndex].Cells[0].Text
NishTrivedi 11-Oct-12 17:10pm
exception message:conversion failed when converting the varchar value 'xyz' to datatype int.
Marcus Kramer 11-Oct-12 17:15pm
Do you not see the problem there? Of course "xyz" won't convert to an int.
NishTrivedi 11-Oct-12 17:11pm

i want to perform this task..
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

private void GridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
           UpdateDatabase ud = new UpdateDatabase();
           ud.Controls["Fname_tbox"].Text = GridView.CurrentRow.Cells[0].Value.ToString();


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 14 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100