Click here to Skip to main content
Rate this: bad
good
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()
        {
            InitializeComponent();
        }
        
        private void show_database_Load(object sender, EventArgs e)
        {
            SqlConnection dbconnection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\edu\\7th sem\\a.net\\assignment\\ass3cs\\ass3cs\\BikeData.mdf;Integrated Security=True;User Instance=True");
            dbconnection.Open();
            
            SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM customer_database", dbconnection);
 
            DataTable dt = new DataTable();
            sda.Fill(dt);
            GridView.DataSource = dt;
        }
 
        private void close_btn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
 
        private void GridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Int id = Convert.ToInt32(GridView.Rows[e.RowIndex].Cells[0].Value);
            UpdateDatabase ud = new UpdateDatabase(id);
            ud.ShowDialog();
        }       
    }
}
Posted 11-Oct-12 10:10am
Edited 11-Oct-12 10:18am
v3
Comments
Wes Aday at 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 at 11-Oct-12 16:04pm
   
Input string is not in correct format.
and sorry..
TryParse does not work.
Wes Aday at 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 at 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 at 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 at 11-Oct-12 16:20pm
   
string contains name value.
Wes Aday at 11-Oct-12 16:22pm
   
And what exactly does that mean? You mean a string containing a name like "Bob"?
NishTrivedi at 11-Oct-12 16:17pm
   
when i use TryParse at that time "No overload for method 'TryParse' takes 1 argument is generate.
Wes Aday at 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 at 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 at 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 at 11-Oct-12 16:56pm
   
Argument 1: cannot convert from 'object' to 'string' error generate.
Wes Aday at 11-Oct-12 17:02pm
   
Then try
Int32.TryParse(GridView.Rows[e.RowIndex].Cells[0].Value.ToString(), out t);
NishTrivedi at 11-Oct-12 17:08pm
   
exception:conversion failed when converting the varchar value 'xyz' to datatype int.
NishTrivedi at 11-Oct-12 17:11pm
   
http://mindstick.com/Articles/924bee57-80b0-4ec7-8cdb-24207ed8d7e9/
 
i want to perform this task.
Wes Aday at 11-Oct-12 17:12pm
   
And? Did you expect something else? Do you think that "xyz" should be an int somehow?
Wes Aday at 11-Oct-12 17:17pm
   
And that is an exception from your database.
NishTrivedi at 12-Oct-12 8:58am
   
yes
ridoy at 11-Oct-12 15:51pm
   
try with int id..in place of Int
NishTrivedi at 11-Oct-12 16:05pm
   
no change..
still same error is generate.
Rate this: bad
good
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;
try
{
   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.
}
  Permalink  
v3
Comments
NishTrivedi at 11-Oct-12 15:53pm
   
hi Marcus,
still the same error is generated using convet.toint32 in try-catch..
Marcus Kramer at 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 at 11-Oct-12 16:54pm
   
Argument 1: cannot convert from 'object' to 'string'
error generate.
Marcus Kramer at 11-Oct-12 16:56pm
   
Show the 1 line of code, please?
NishTrivedi at 11-Oct-12 16:57pm
   
this line creates the error:
GridView.Rows[e.RowIndex].Cells[0].Value
Marcus Kramer at 11-Oct-12 17:08pm
   
Try GridView.Rows[e.RowIndex].Cells[0].Value.ToString() or GridView.Rows[e.RowIndex].Cells[0].Text
NishTrivedi at 11-Oct-12 17:10pm
   
exception message:conversion failed when converting the varchar value 'xyz' to datatype int.
Marcus Kramer at 11-Oct-12 17:15pm
   
Do you not see the problem there? Of course "xyz" won't convert to an int.
NishTrivedi at 11-Oct-12 17:11pm
   
http://mindstick.com/Articles/924bee57-80b0-4ec7-8cdb-24207ed8d7e9/
 
i want to perform this task..
Rate this: bad
good
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();
           ud.Show();
 
       }
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.150301.1 | Last Updated 14 Oct 2012
Copyright © CodeProject, 1999-2015
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